| |||||||
| Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics. |
![]() |
| | LinkBack | Thread Tools |
| | #1 (permalink) |
| Account Closed | Simple PHP help needed. <? $sql2 = "SELECT * FROM games WHERE visible = '1' ORDER BY RAND() LIMIT 4"; $result2 = mysql_query($sql2); while($row2 = mysql_fetch_array($result2)) { ?> I salvaged this code from my old site I had coded for me. So, if I bascially need something like the above but.. I need to do this. I have a users table which contains the fields artist1-10 and song1-10. I want to select the artists, randomly like the above and echo them out like this: Popular artists/bands. <?php echo " artist1 artist2 artist3 ?> etc, 8 times down, if anyone can do this for me, I'll buy some NP's in the next week and donate a few too you. EDIT: so basically inside my users table, i have usernames and then artist1-10 and song 1-10 for every username, i want to randomly select these artists and display them Last edited by electricbeat; 07-08-2006 at 07:39 AM. |
| |
| | #2 (permalink) |
| Senior Member | I'm assuming from the table structure you described that username is the first field, then artists are the next 10 fields, then songs are the next 10 fields. If not you will encounter errors with this. This will pick 8 usernames, and display the 10 artists/10 songs of each. Code: <?
$sql = "SELECT * FROM users WHERE ORDER BY RAND() LIMIT 8";
$result = mysql_query($sql);
if ($result) {
// This check is to make sure you made a succesful query before you continue
while($row = mysql_fetch_array($result2)) {
echo "Username: $row[0]<br>
Artist 1: $row[1]<br>
Artist 2: $row[2]<br>
Artist 3: $row[3]<br>
Artist 4: $row[4]<br>
Artist 5: $row[5]<br>
Artist 6: $row[6]<br>
Artist 7: $row[7]<br>
Artist 8: $row[8]<br>
Artist 9: $row[9]<br>
Artist 10: $row[10]<br>
Song 1: $row[11]<br>
Song 2: $row[12]<br>
Song 3: $row[13]<br>
Song 4: $row[14]<br>
Song 5: $row[15]<br>
Song 6: $row[16]<br>
Song 7: $row[17]<br>
Song 8: $row[18]<br>
Song 9: $row[19]<br>
Song 10: $row[20]<br><hr>";
} // End while
} else {
echo "The query wasn't successful.";
}
?>
|
| |
| | #3 (permalink) |
| Account Closed | Sounds like that I needed, i'll just test it, thank you! The query was unsuccessful it replied with. Too give you a better idea of my db this is it. http://img89.imageshack.us/img89/793/png4ja.png So, this is inside users. I have duplicate entries because i was testing pagination and other things. Last edited by electricbeat; 07-08-2006 at 07:56 AM. |
| |
| | #4 (permalink) |
| Senior Member | Oh ok, that makes things a little more awkward, but all it requires is some moving around of numbers. From the image i'm assuming from then on, it goes "artist3, song3, artist4, song4". Code: <?
// I've never seen this ORDER BY RAND() before, I hope it works for you...
$sql = "SELECT * FROM users ORDER BY RAND() LIMIT 8";
$result = mysql_query($sql);
if ($result) {
// This check is to make sure you made a succesful query before you continue
while($row = mysql_fetch_array($result)) {
echo "Username: $row[1]<br>
Artist 1: $row[5]<br>
Artist 2: $row[7]<br>
Artist 3: $row[9]<br>
Artist 4: $row[11]<br>
Artist 5: $row[13]<br>
Artist 6: $row[15]<br>
Artist 7: $row[17]<br>
Artist 8: $row[19]<br>
Artist 9: $row[21]<br>
Artist 10: $row[23]<br>
Song 1: $row[6]<br>
Song 2: $row[8]<br>
Song 3: $row[10]<br>
Song 4: $row[12]<br>
Song 5: $row[14]<br>
Song 6: $row[16]<br>
Song 7: $row[18]<br>
Song 8: $row[20]<br>
Song 9: $row[22]<br>
Song 10: $row[24]<br><hr>";
} // End while
} else {
echo "The query wasn't successful.";
}
?>
Last edited by Shorty; 07-08-2006 at 08:23 AM. |
| |
| | #6 (permalink) |
| I'll do it Technical Services | PHP Code:
__________________ Vhuv|Delq|Fhur|Twut|Tluz|Kegh|Vhir|Juhy|Ruuz|Jyos|Jupt|Vhek Webmaster Blog | Software Downloads Sponsor Ads at SponAds.com |
| |
| | #16 (permalink) |
| Account Closed | I'm needing more help again :P PHP Code: |
| |
| | #17 (permalink) |
| Senior Member | Code: if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,6})", $email)) {
// The email isnt ok, print an error message
} else {
// The email is fine, so carry on
// And this last bracket goes at the end of your script
}
|
| |
| | #18 (permalink) |
| NamePros Regular | ([_\.0-9a-z-]+@) ([_\.0-9a-z-]+)@ try chaning that
__________________ Site Armory, tools for your website | Webmaster SEO Forums | PHP Tutorial and coding forums |
| |
| | #19 (permalink) |
| Account Closed | My friend gave me this: PHP Code: |
| |
| | #20 (permalink) |
| NamePros Member | Just a tip, you should really indent your code. It's kind of hard to follow your code when there's no indentation.... can't tell where if...else statements start and end. As far as your issue, in the code you just posted that your friend gave you, he created a variable "$bIsValid" - it's set to false if the email address isn't valid. So just before issuing your registration query, just do an if block Code: if ($bIsValid == true) {
...
}
__________________ |
| |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| |