Alphabetical sorting help....

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post by smpdawg »

Post the current version of the script.
Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

Post by Smackie »

Code: Select all

<?
if ((isset($_GET&#1111;'uname']) == true) && (isset($_GET&#1111;'poem']) == true)) 
&#123; 

// Tell the user it has been submitted (optional) 
echo('Your comment has been posted.');

// Set Mysql Variables
$host = 'localhost'; 
$user = '***';
$pass = '***';
$db = 'haunted_poetry';
$table = 'dbpoems'; 

// Set global variables to easier names
$username = $_GET&#1111;'username']; 
$poem = $_GET&#1111;'poem']; 

// Connect to Mysql, select the correct database, and run teh query which adds the data gathered from the form into the database
mysql_connect($host,$user,$pass) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());
$add_all = "INSERT INTO $table values("$username","$poem")"; 
echo $add_all; 
mysql_query($add_all) or die(mysql_error()); 
&#125;
else
&#123;
echo $add_all;
// If the form has not been submitted, display it!
?>
<form method='get' action='<? echo $PHP_SELF; ?>'>
Username: <input type='text' name='username'><br><br>
Poem: <input type='text' name='poem'><br><br>
<input type='submit' value='Post your poems'>
</form> 
<?
&#125;
?>
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post by smpdawg »

You missed the uname on line 2, it needs to be username too.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

still referencing uname and username...

register_globals is on right?
Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

Post by Smackie »

alright this is what i get now
Your comment has been posted.
Warning: mysql_connect(): Access denied for user: 'user@localhost' (Using password: YES) in /home/haunted/public_html/Poetry/poetry.php on line 20
Access denied for user: 'user@localhost' (Using password: YES)
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post by smpdawg »

Either your MySQL username or password is wrong.

The default is user root and no password but your host more than likely gave you a specific MySQL username and password and in many cases it is different than your email user/password or ftp user/password.
Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

Post by Smackie »

ok that is posting in Database now... thank you on that......................
now why doesnt it post in where i want it to be posted into now?
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post by smpdawg »

What do you mean? What isn't working?
Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

Post by Smackie »

alright well see when the user submits his/her poem it goes to the database well then it goes to poem.php page and outputs in alphabetical order i have the script and somewhat works but only thing thats not working is showing the poems on it or the username :cry:
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post by smpdawg »

Have you added the code to poem.php to walk through the list of users, etc and show them?
Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

Post by Smackie »

i think so im not for sure :cry: but here is the script for poem.php

Code: Select all

<?php 

// This function makes the alphabetic list which the links. 
function drawAddressList() &#123; 
  $seperator = "-"; 

  // Loop through all 26 characters. 
  for ($i = 1; $i <= 26; $i++) &#123; 
    $letter = chr($i + 64); 
    echo $seperator . "<a href='&#123;$_SERVER&#1111;'PHP_SELF']&#125;?letter=&#123;$letter&#125;'>&#123;$letter&#125;</a>"; 
  &#125;  
  echo $seperator . '<br />'; 
&#125; 

// This function gets a parameter or the default. 
function getParm($VariableName, $Default = '') &#123; 
  return (isset($_GET&#1111;$VariableName])) ? $_GET&#1111;$VariableName] : $Default;    
&#125; 

drawAddressList(); 

// Connect to MySQL - use your user name and password. 
$link = mysql_connect('localhost', 'username', 'password'); 
if (!$link) &#123; 
   die('Could not connect: ' . mysql_error()); 
&#125; 

// Connects to the database that has the user table. 
$db_selected = mysql_select_db('db', $link); 
if (!$db_selected) &#123; 
   die ('Can''t use table : ' . mysql_error()); 
&#125; else &#123; 
  $letter = getParm('letter'); 

  if ($letter != '') &#123; 
    $letter_query = "SELECT * FROM `table` WHERE `username` REGEXP '^$letter.*' ORDER BY `username`"; 
    $letter_result = mysql_query($letter_query); 
    if ($letter_result != false) &#123; 
       while ($letter_row = mysql_fetch_assoc($letter_result)) &#123; 
         // Do something in here with the rows you get. 
          // Maybe use the information from the user table to fill another recordset with the poems. 
       &#125; 
    &#125; 
  &#125;  
&#125; 

?>
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post by smpdawg »

Two things.

You need to fix the database connection information (like user, password, database) in this script and also put some code inside the while that will display the information from the database. When I made this sample, I didn't add DB information because I didn't have it. Start by fixing this stuff up.

Code: Select all

// Connect to MySQL - use your user name and password.
$link = mysql_connect('localhost', 'username', 'password');
if (!$link) &#123;
   die('Could not connect: ' . mysql_error());
&#125;

// Connects to the database that has the user table.
$db_selected = mysql_select_db('db', $link);
if (!$db_selected) &#123;
   die ('Can''t use table : ' . mysql_error());
&#125; else &#123;
  $letter = getParm('letter');

  if ($letter != '') &#123;
    $letter_query = "SELECT * FROM `table` WHERE `username` REGEXP '^$letter.*' ORDER BY `username`";
    $letter_result = mysql_query($letter_query);
    if ($letter_result != false) &#123;
       while ($letter_row = mysql_fetch_assoc($letter_result)) &#123;
         // Do something in here with the rows you get.
          // Maybe use the information from the user table to fill another recordset with the poems.
       &#125;
Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

Post by Smackie »

well that script you just showed was already in the script..
<?php

// This function makes the alphabetic list which the links.
function drawAddressList() {
$seperator = "-";

// Loop through all 26 characters.
for ($i = 1; $i <= 26; $i++) {
$letter = chr($i + 64);
echo $seperator . "<a href='{$_SERVER['PHP_SELF']}?letter={$letter}'>{$letter}</a>";
}
echo $seperator . '<br />';
}

// This function gets a parameter or the default.
function getParm($VariableName, $Default = '') {
return (isset($_GET[$VariableName])) ? $_GET[$VariableName] : $Default;
}

drawAddressList();

// Connect to MySQL - use your user name and password.
$link = mysql_connect('localhost', 'username', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}

// Connects to the database that has the user table.
$db_selected = mysql_select_db('db', $link);
if (!$db_selected) {
die ('Can''t use table : ' . mysql_error());
} else {
$letter = getParm('letter');

if ($letter != '') {
$letter_query = "SELECT * FROM `table` WHERE `username` REGEXP '^$letter.*' ORDER BY `username`";
$letter_result = mysql_query($letter_query);
if ($letter_result != false) {
while ($letter_row = mysql_fetch_assoc($letter_result)) {
// Do something in here with the rows you get.
// Maybe use the information from the user table to fill another recordset with the poems.
}

}
}
}

?>
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post by smpdawg »

You are missing my point, you haven't actually put the user name, password, database name, table name, etc in the script so it won't work properly.
Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

Post by Smackie »

i have put that stuff in there if i didnt it would look like this..
-A-B-C-D-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z-

Warning: mysql_connect(): Access denied for user: 'username@localhost' (Using password: YES) in /home/haunted/public_html/Poetry/test.php on line 23
Could not connect: Access denied for user: 'username@localhost' (Using password: YES)
but since i did ti looks like this
-A-B-C-D-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z-
here is both links so you can see for yourself
*the script with the username, password, database selected*
http://www.hauntedgraveyard.net/Poetry/poems.php
*the script without the username, password, database selected*
http://www.hauntedgraveyard.net/Poetry/test.php
Post Reply