[advanced search]
 

Go Back   NamePros.com > Discussion > Web Design & Development > Programming

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


Closed Thread
 
LinkBack Thread Tools
Old 07-08-2006, 07:17 AM   #1 (permalink)
Account Closed
 
Join Date: Jul 2006
Posts: 81
104.40 NP$ (Donate)

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

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.
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, 07:51 AM   #3 (permalink)
Account Closed
 
Join Date: Jul 2006
Posts: 81
104.40 NP$ (Donate)

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 07:56 AM.
electricbeat is offline  
Old 07-08-2006, 08:02 AM   #4 (permalink)
Senior Member
 
Shorty's Avatar
 
Join Date: Sep 2005
Location: England
Posts: 1,035
102.05 NP$ (Donate)

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 08:23 AM.
Shorty is offline  
Old 07-08-2006, 08:09 AM   #5 (permalink)
Account Closed
 
Join Date: Jul 2006
Posts: 81
104.40 NP$ (Donate)

electricbeat is an unknown quantity at this point


Hmm, still doesn't seem to be working.
electricbeat is offline  
Old 07-08-2006, 08:16 AM   #6 (permalink)
I'll do it

Technical Services

 
-Nick-'s Avatar
 
Join Date: Dec 2005
Location: India
Posts: 6,434
5,169.80 NP$ (Donate)

-Nick- has a reputation beyond repute-Nick- has a reputation beyond repute-Nick- has a reputation beyond repute-Nick- has a reputation beyond repute-Nick- has a reputation beyond repute-Nick- has a reputation beyond repute-Nick- has a reputation beyond repute-Nick- has a reputation beyond repute-Nick- has a reputation beyond repute-Nick- has a reputation beyond repute-Nick- has a reputation beyond repute

Adoption
PHP Code:
<?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
?>
-Nick- is online now  
Old 07-08-2006, 08:22 AM   #7 (permalink)
Senior Member
 
Shorty's Avatar
 
Join Date: Sep 2005
Location: England
Posts: 1,035
102.05 NP$ (Donate)

Shorty is just really niceShorty is just really niceShorty is just really niceShorty is just really nice


Quote:
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, 08:24 AM   #8 (permalink)
Account Closed
 
Join Date: Jul 2006
Posts: 81
104.40 NP$ (Donate)

electricbeat is an unknown quantity at this point


Quote:
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, 08:27 AM   #9 (permalink)
Senior Member
 
Shorty's Avatar
 
Join Date: Sep 2005
Location: England
Posts: 1,035
102.05 NP$ (Donate)

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, 08:39 AM   #10 (permalink)
Account Closed
 
Join Date: Jul 2006
Posts: 81
104.40 NP$ (Donate)

electricbeat is an unknown quantity at this point


yup, connected to db.
electricbeat is offline  
Old 07-08-2006, 08:40 AM   #11 (permalink)
Senior Member
 
Shorty's Avatar
 
Join Date: Sep 2005
Location: England
Posts: 1,035
102.05 NP$ (Donate)

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, 08:58 AM   #12 (permalink)
Account Closed
 
Join Date: Jul 2006
Posts: 81
104.40 NP$ (Donate)

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, 09:12 AM   #13 (permalink)
Senior Member
 
Shorty's Avatar
 
Join Date: Sep 2005
Location: England
Posts: 1,035
102.05 NP$ (Donate)

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, 07:56 PM   #14 (permalink)
Senior Member
 
Join Date: May 2005
Location: Ontario Canada
Posts: 2,928
1,675.13 NP$ (Donate)

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 behold


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

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, 04:44 AM   #16 (permalink)
Account Closed
 
Join Date: Jul 2006
Posts: 81
104.40 NP$ (Donate)

electricbeat is an unknown quantity at this point


I'm needing more help again :P

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]) {
// 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'.
electricbeat is offline  
Old 07-09-2006, 04:51 AM   #17 (permalink)
Senior Member
 
Shorty's Avatar
 
Join Date: Sep 2005
Location: England
Posts: 1,035
102.05 NP$ (Donate)

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, 04:52 AM   #18 (permalink)
NamePros Regular
 
-PS-'s Avatar
 
Join Date: Jun 2006
Location: Sydney
Posts: 251
5.95 NP$ (Donate)

-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, 05:09 AM   #19 (permalink)
Account Closed
 
Join Date: Jul 2006
Posts: 81
104.40 NP$ (Donate)

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;
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, 07:57 AM   #20 (permalink)
NamePros Member
 
CreedFeed's Avatar
 
Join Date: Apr 2006
Posts: 186
195.95 NP$ (Donate)

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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Site Sponsors
Advertise your business at NamePros

All times are GMT -7. The time now is 04:01 AM.


Powered by: vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0
Template-Modifications by TMS
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85