NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page Simple PHP help needed.

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 07-08-2006, 08:17 AM THREAD STARTER               #1 (permalink)
Account Closed
Join Date: Jul 2006
Posts: 81
electricbeat is an unknown quantity at this point
 



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 08:39 AM.
electricbeat is offline  
Old 07-08-2006, 08:49 AM   #2 (permalink)
Senior Member
 
Shorty's Avatar
Join Date: Sep 2005
Location: England
Posts: 1,034
Shorty is just really niceShorty is just really niceShorty is just really niceShorty is just really nice
 



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.
????: NamePros.com http://www.namepros.com/programming/214765-simple-php-help-needed.html
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.
Shorty is offline  
Old 07-08-2006, 08:51 AM THREAD STARTER               #3 (permalink)
Account Closed
Join Date: Jul 2006
Posts: 81
electricbeat is an unknown quantity at this point
 



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 08:56 AM.
electricbeat is offline  
Old 07-08-2006, 09:02 AM   #4 (permalink)
Senior Member
 
Shorty's Avatar
Join Date: Sep 2005
Location: England
Posts: 1,034
Shorty is just really niceShorty is just really niceShorty is just really niceShorty is just really nice
 



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 09:23 AM.
Shorty is offline  
Old 07-08-2006, 09:09 AM THREAD STARTER               #5 (permalink)
Account Closed
Join Date: Jul 2006
Posts: 81
electricbeat is an unknown quantity at this point
 



Hmm, still doesn't seem to be working.
electricbeat is offline  
Old 07-08-2006, 09:16 AM   #6 (permalink)
I'll do it
 
-Nick-'s Avatar
Join Date: Dec 2005
Location: India
Posts: 6,927
-Nick- Has achieved greatness-Nick- Has achieved greatness-Nick- Has achieved greatness-Nick- Has achieved greatness-Nick- Has achieved greatness-Nick- Has achieved greatness-Nick- Has achieved greatness-Nick- Has achieved greatness-Nick- Has achieved greatness-Nick- Has achieved greatness-Nick- Has achieved greatness
 


Member of the Month
September 2007
Adoption
PHP Code:
<?php
$r 
mysql_query("SELECT * FROM users ORDER BY RAND() LIMIT 8");
????: NamePros.com http://www.namepros.com/showthread.php?t=214765
while( 
$row mysql_fetch_array$r ) )
{
echo 
$row['artist1'];
echo 
"--";
echo 
$row['artist2'];
echo 
"<br>";
//While ends
?>
-Nick- is offline  
Old 07-08-2006, 09:22 AM   #7 (permalink)
Senior Member
 
Shorty's Avatar
Join Date: Sep 2005
Location: England
Posts: 1,034
Shorty is just really niceShorty is just really niceShorty is just really niceShorty is just really nice
 



Originally Posted by electricbeat
Hmm, still doesn't seem to be working.
Did the same error message appear?
Shorty is offline  
Old 07-08-2006, 09:24 AM THREAD STARTER               #8 (permalink)
Account Closed
Join Date: Jul 2006
Posts: 81
electricbeat is an unknown quantity at this point
 



Originally Posted by Shorty
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
electricbeat is offline  
Old 07-08-2006, 09:27 AM   #9 (permalink)
Senior Member
 
Shorty's Avatar
Join Date: Sep 2005
Location: England
Posts: 1,034
Shorty is just really niceShorty is just really niceShorty is just really niceShorty is just really nice
 



...silly question. But are you connecting to the database before you implement this code?
Shorty is offline  
Old 07-08-2006, 09:39 AM THREAD STARTER               #10 (permalink)
Account Closed
Join Date: Jul 2006
Posts: 81
electricbeat is an unknown quantity at this point
 



yup, connected to db.
electricbeat is offline  
Old 07-08-2006, 09:40 AM   #11 (permalink)
Senior Member
 
Shorty's Avatar
Join Date: Sep 2005
Location: England
Posts: 1,034
Shorty is just really niceShorty is just really niceShorty is just really niceShorty is just really nice
 



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. =)
Shorty is offline  
Old 07-08-2006, 09:58 AM THREAD STARTER               #12 (permalink)
Account Closed
Join Date: Jul 2006
Posts: 81
electricbeat is an unknown quantity at this point
 



It's working! Thank you shorty, I've added you too a list of whom I own NP too.
electricbeat is offline  
Old 07-08-2006, 10:12 AM   #13 (permalink)
Senior Member
 
Shorty's Avatar
Join Date: Sep 2005
Location: England
Posts: 1,034
Shorty is just really niceShorty is just really niceShorty is just really niceShorty is just really nice
 



Ahhh thats good news, I knew I wasn't far off the solution.

Good luck with the code and the site too.
Shorty is offline  
Old 07-08-2006, 08:56 PM   #14 (permalink)
Senior Member
Join Date: May 2005
Location: Ontario Canada
Posts: 3,088
unknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to behold
 


Diabetes
just wondering
instead of listing
blah[1]
blah[2]
......
cant u do it in a for loop?
unknowngiver is offline  
Old 07-09-2006, 05:23 AM   #15 (permalink)
Senior Member
 
Shorty's Avatar
Join Date: Sep 2005
Location: England
Posts: 1,034
Shorty is just really niceShorty is just really niceShorty is just really niceShorty is just really nice
 



It's already in a while () loop...what's the difference?
Shorty is offline  
Old 07-09-2006, 05:44 AM THREAD STARTER               #16 (permalink)
Account Closed
Join Date: Jul 2006
Posts: 81
electricbeat is an unknown quantity at this point
 



I'm needing more help again

PHP Code:
<!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]) {
????: NamePros.com http://www.namepros.com/showthread.php?t=214765
// 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];
????: NamePros.com http://www.namepros.com/showthread.php?t=214765
$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'");
$checknamemysql_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'.
electricbeat is offline  
Old 07-09-2006, 05:51 AM   #17 (permalink)
Senior Member
 
Shorty's Avatar
Join Date: Sep 2005
Location: England
Posts: 1,034
Shorty is just really niceShorty is just really niceShorty is just really niceShorty is just really nice
 



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.
Shorty is offline  
Old 07-09-2006, 05:52 AM   #18 (permalink)
NamePros Regular
 
-PS-'s Avatar
Join Date: Jun 2006
Location: Sydney
Posts: 251
-PS- will become famous soon enough-PS- will become famous soon enough
 



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

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



try chaning that
-PS- is offline  
Old 07-09-2006, 06:09 AM THREAD STARTER               #19 (permalink)
Account Closed
Join Date: Jul 2006
Posts: 81
electricbeat is an unknown quantity at this point
 



My friend gave me this:

PHP Code:
$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;
????: NamePros.com http://www.namepros.com/showthread.php?t=214765
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
electricbeat is offline  
Old 07-09-2006, 08:57 AM   #20 (permalink)
NamePros Member
 
CreedFeed's Avatar
Join Date: Apr 2006
Posts: 186
CreedFeed is on a distinguished road
 



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) {
...
}
CreedFeed is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Liquid Web Smart Servers  
All times are GMT -7. The time now is 09:16 PM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger