NameSilo

Simple PHP help needed.

Spaceship Spaceship
Watch

electricbeat

Account Closed
Impact
0
<?
$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:
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
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.";

}
?>
Reply if it doesn't work properly and i'll fix it. Good luck.
 
0
•••
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:
0
•••
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:
0
•••
Hmm, still doesn't seem to be working.
 
0
•••
PHP:
<?php
$r = mysql_query("SELECT * FROM users ORDER BY RAND() LIMIT 8");
while( $row = mysql_fetch_array( $r ) )
{
echo $row['artist1'];
echo "--";
echo $row['artist2'];
echo "<br>";
} //While ends
?>
 
0
•••
electricbeat said:
Hmm, still doesn't seem to be working.
Did the same error message appear?
 
0
•••
Shorty said:
Did the same error message appear?

No I got: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/ryan/public_html/index.php on line 67
 
0
•••
...silly question. But are you connecting to the database before you implement this code?
 
0
•••
yup, connected to db.
 
0
•••
Ok, I made a brief adjustment to my latest code above, can you try that please? I'm too embarassed to say what a foolish error I made. =)
 
0
•••
It's working! Thank you shorty, I've added you too a list of whom I own NP too.
 
0
•••
Ahhh thats good news, I knew I wasn't far off the solution. ;)

Good luck with the code and the site too.
 
0
•••
just wondering
instead of listing
blah[1]
blah[2]
......
cant u do it in a for loop?
 
0
•••
It's already in a while () loop...what's the difference?
 
0
•••
I'm needing more help again :P

PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html;charset=iso-8859-2" />
<link rel="stylesheet" href="images/style.css" type="text/css" />
<title>MusicInterests</title>
</head>
<body>
	<div class="content">
		<div class="header_right">
			<div class="top_info">
				<div class="top_info_right">
					<p><?php include("logged.php"); ?></p>					
				</div>		
			</div>
					
			<div class="bar">
				<ul>
					<li class="slogan">Navigation:</li>
<?php include("navigation.php"); ?>
				
				</ul>
			</div>
		</div>
			
		<div class="logo">
			<h1><a href="#" title="The best music on the net!">Music <span class="red">Interests</span></a></h1>
			<p>What are you listening too?</p>
		</div>
		
		<div class="search_field">
			<?php include("searchform.php"); ?>
		</div>
		
		<div class="newsletter">
			<p>Subscribe for the Newsletter!</p>
		</div>
		
		<div class="subheader">
			<p><?php include("top.php"); ?></p>
		</div>
		
		<div class="left">
			<div class="left_articles">
				<h2>Sign up!</h2>
				<p><?php
ob_start();
// allows you to use cookies
include("config.php");
//gets the config page
if ($_POST[register]) {
// the above line checks to see if the html form has been submitted
$username = $_POST[username];
$password = $_POST[pass];
$cpassword = $_POST[cpass];
$email = $_POST[emai1];
if(!(eregi( "([_\.0-9a-z-]+@)([0-9a-z][0-9a-z-]+\.)+([a-z]{2,3})", $email)))
    {
        //not a valid email
    } 
$artist1 = $_POST[artist1];
$song1 = $_POST[song1];
$artist2 = $_POST[artist2];
$song2 = $_POST[song2];
$artist3 = $_POST[artist3];
$song3 = $_POST[song3];
$artist4 = $_POST[artist4];
$song4 = $_POST[song4];
$artist5 = $_POST[artist5];
$song5 = $_POST[song5];
$artist6 = $_POST[artist6];
$song6 = $_POST[song6];
$artist7 = $_POST[artist7];
$song7 = $_POST[song7];
$artist8 = $_POST[artist8];
$song8 = $_POST[song8];
$artist9 = $_POST[artist9];
$song9 = $_POST[song9];
$artist10 = $_POST[artist10];
$song10 = $_POST[song10];
//the above lines set variables with the user submitted information
if($username==NULL|$password==NULL|$cpassword==NULL|$email==NULL|$artist1==NULL|$song1==NULL|$artist2==NULL|$song2==NULL|$artist3==NULL|$song3==NULL|$artist4==NULL|$song4==NULL|$artist5==NULL|$song5==NULL|$artist6==NULL|$song6==NULL|$artist7==NULL|$song7==NULL|$artist8==NULL|$song8==NULL|$artist9==NULL|$song9==NULL|$artist10==NULL|$song10==NULL) {
//checks to make sure no fields were left blank
echo "<font color=\"red\">A field was left blank or you did not enter all your songs.<br /><br />
<a href=\"#\" onClick=\"history.go(-1)\">Click here to go back</a> 
</font>
";
}else{
//none were left blank!  We continue...
if($password != $cpassword) {
// the passwords are not the same!  
echo "<font color=\"red\">Passwords do not match.</font> <br /><br />
<a href=\"#\" onClick=\"history.go(-1)\">Click here to go back</a>
";
}else{
// the passwords are the same!  we continue...
$password = md5($password);

// encrypts the password
$checkname = mysql_query("SELECT username FROM users WHERE username='$username'");
$checkname= mysql_num_rows($checkname);
$checkemail = mysql_query("SELECT email FROM users WHERE email='$email'");
$checkemail = mysql_num_rows($checkemail);
if ($checkemail>0|$checkname>0) {

// oops...someone has already registered with that username or email!
echo "<font color=\"red\">The username or email is already in use.</font><br /><br />
<a href=\"#\" onClick=\"history.go(-1)\">Click here to go back</a> 
";
}else{
// noone is using that email or username!  We continue...
$username = htmlspecialchars($username);
$password = htmlspecialchars($password);
$email = htmlspecialchars($email);
$song1 = htmlspecialchars($song1);
$artist1 = htmlspecialchars($artist1);
$song2 = htmlspecialchars($song2);
$artist2 = htmlspecialchars($artist2);
$song3 = htmlspecialchars($song3);
$artist3 = htmlspecialchars($artist3);
$song4 = htmlspecialchars($song4);
$artist4 = htmlspecialchars($artist4);
$song5 = htmlspecialchars($song5);
$artist5 = htmlspecialchars($artist5);
$song6 = htmlspecialchars($song6);
$artist6 = htmlspecialchars($artist6);
$song7 = htmlspecialchars($song7);
$artist7 = htmlspecialchars($artist7);
$song8 = htmlspecialchars($song8);
$artist8 = htmlspecialchars($artist8);
$song9 = htmlspecialchars($song9);
$artist9 = htmlspecialchars($artist9);
$song10 = htmlspecialchars($song10);
$artist10 = htmlspecialchars($artist10);
// the above lines make it so that there is no html in the user submitted information.
//Everything seems good, lets insert.
$query = mysql_query("INSERT INTO users (username, password, email, song1, artist1, song2, artist2, song3, artist3, song4, artist4, song5, artist5, song6, artist6, song7, artist7, song8, artist8, song9, artist9, song10, artist10) VALUES('$username','$password','$email','$song1','$artist1','$song2','$artist2','$song3','$artist3','$song4','$artist4','$song5','$artist5','$song6','$artist6','$song7','$artist7','$song8','$artist8','$song9','$artist9','$song10','$artist10')");
// inserts the information into the database.
echo "You have successfully registered!";
}
}
}
}
else
{
// the form has not been submitted...so now we display it.
echo ("

<form method=\"POST\">
Username:<br><input type=\"text\" size=\"15\" maxlength=\"25\" name=\"username\"><br />
Password:<br><input type=\"password\" size=\"15\" maxlength=\"25\" name=\"pass\"><br />
Confirm Password:<br><input type=\"password\" size=\"15\" maxlength=\"25\" name=\"cpass\"><br />
Email:<br><input type=\"text\" size=\"15\" maxlength=\"25\" name=\"emai1\"><br /><br />
Please add your songs, you can always go back and edit or change them if you make a mistake or update your collection. Please spell the artist and songs as well as possible.<br><br>
<b>Song one</b><br>
Artist - <input type=\"text\" size=\"19\" maxlength=\"25\" name=\"artist1\"> Song - <input type=\"text\" size=\"19\" maxlength=\"25\" name=\"song1\"><br />
<b>Song two</b><br>
Artist - <input type=\"text\" size=\"19\" maxlength=\"25\" name=\"artist2\"> Song - <input type=\"text\" size=\"19\" maxlength=\"25\" name=\"song2\"><br />
<b>Song three</b><br>
Artist - <input type=\"text\" size=\"19\" maxlength=\"25\" name=\"artist3\"> Song - <input type=\"text\" size=\"19\" maxlength=\"25\" name=\"song3\"><br />
<b>Song four</b><br>
Artist - <input type=\"text\" size=\"19\" maxlength=\"25\" name=\"artist4\"> Song - <input type=\"text\" size=\"19\" maxlength=\"25\" name=\"song4\"><br />
<b>Song five</b><br>
Artist - <input type=\"text\" size=\"19\" maxlength=\"25\" name=\"artist5\"> Song - <input type=\"text\" size=\"19\" maxlength=\"25\" name=\"song5\"><br />
<b>Song six</b><br>
Artist - <input type=\"text\" size=\"19\" maxlength=\"25\" name=\"artist6\"> Song - <input type=\"text\" size=\"19\" maxlength=\"25\" name=\"song6\"><br />
<b>Song seven</b><br>
Artist - <input type=\"text\" size=\"19\" maxlength=\"25\" name=\"artist7\"> Song - <input type=\"text\" size=\"19\" maxlength=\"25\" name=\"song7\"><br />
<b>Song eight</b><br>
Artist - <input type=\"text\" size=\"19\" maxlength=\"25\" name=\"artist8\"> Song - <input type=\"text\" size=\"19\" maxlength=\"25\" name=\"song8\"><br />
<b>Song nine</b><br>
Artist - <input type=\"text\" size=\"19\" maxlength=\"25\" name=\"artist9\"> Song - <input type=\"text\" size=\"19\" maxlength=\"25\" name=\"song9\"><br />
<b>Song ten</b><br>
Artist - <input type=\"text\" size=\"19\" maxlength=\"25\" name=\"artist10\"> Song - <input type=\"text\" size=\"19\" maxlength=\"25\" name=\"song10\"><br />

<br /><input name=\"register\" type=\"submit\" value=\"Register\">
</form>

");
}
?> 
</p>
			</div>
		</div>	
		<div class="right">
						
			<div class="rt"></div>
			<div class="right_articles">
				<p><b>Most viewed</b><br />The most viewed tool allows you too find the 30 most viewed profiles on musicinterests!</p>
			</div>
			<div class="rt"></div>
			<div class="right_articles">
				<p><b>Links</b><br />
				<?php include("links.php"); ?></p>
			</div>
			<div class="rt"></div>
			<div class="right_articles">
				<p><?php include("sponsor.php"); ?></p>
			</div>
		</div>	
		<div class="footer">
			<p><?php include("footer.php"); ?></p>
		</div>
	</div>
</body>
</html>

So, this is my register.php - can anyone help me in making users have valid emails. At the moment people can register with emails such as 'hello'.
 
0
•••
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
}

Take this code and replace it with your eregi expression.
 
0
•••
([_\.0-9a-z-]+@)

([_\.0-9a-z-]+)@



try chaning that
 
0
•••
My friend gave me this:

PHP:
$sChars = "^[A-Za-z0-9\._-]+@([A-Za-z][A-Za-z0-9-]{1,62})(\.[A-Za-z][A-Za-z0-9-]{1,62})+$";
$bIsValid = true;
if(!ereg("$sChars",$email)){
$bIsValid = false;
echo("<font color=\"red\">Please enter a valid email.<br /><br />
<a href=\"#\" onClick=\"history.go(-1)\">Click here to go back</a> 
</font>");
}

It works, but .. if I echo the message out, the script carries out registering them. If I die(text); it out, then it stops them registering, but stops the rest of the page loading, which I can't have. Any ideas
 
0
•••
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) {
...
}
 
0
•••
Dynadot โ€” .com Registration $8.99Dynadot โ€” .com Registration $8.99
Appraise.net
Unstoppable Domains
Domain Recover
DomainEasy โ€” Payment Flexibility
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back