Jump to content

[SOLVED] help with alphabetical pagination please


CodeMama

Recommended Posts

I searched and tried a hundred different things ...I can't seem to get my pages to load the restaurants of all one letter...my script is all piecemeal and I have to end of day to get it right...someone please have pity on me!! What I have so far....

     if (!isset($_GET['letter'])) {$letter = "A";} else {$letter = $_GET['letter'];
                                                                        $sql="SELECT * FROM restaurants WHERE name LIKE '".$letter."%'";
                                                                         $result = mysql_query($sql) or die(mysql_error()) ;
                                                                         echo "
                                                                                    <div align=left width=100>
                                                                                    <b>$result[name]</b><br>
                                                                                   
                                                                                    $result[address]</br>
                                                                                    $result[cviolations]</br>
                                                                                   
                                                                                    </div>
                                                                                    <hr color=#000 width=200></hr>
                                                                                    ";
                                                                         
                                                                        }
                                      // List not grabbed yet, so run query and store in $_SESSION

                                                                     //alphabetical pagination links
                                                                    if (!isset($_GET['letter'])) {$letter = "A";} else {$letter = $_GET['letter'];}
                                                                    echo '<div align="center"><b>';
                                                                    for ($i=65; $i<90; $i++) {
                                                                    if ($letter!= chr($i)) {echo '<a href="browse.php?letter='.chr($i).'">';}
                                                                    echo chr($i)." ";
                                                                    if ($letter!= chr($i)) {echo '</a>';
                                                                    
                                                                    
                                                                    
                                                                    
                                                                    
                                                                    
                                                                            }
                                                                    }
                                                                    echo "</b></div><p>";
                                                                    
                                                                    $sql="SELECT * FROM restaurants WHERE name LIKE '".$letter."%'"; 
                                                                    $query="SELECT * FROM songs WHERE UPPER(SUBSTRING(name,1,1)) NOT BETWEEN 'A' and 'Z'"; 

                                                               $sql = "SELECT DISTINCT name FROM restaurants WHERE name LIKE
                                                                          '$letter.%' GROUP BY name DESC";
                                                            
                                                                $result = mysql_query($sql) or die(mysql_error()) ;
                                                                $count = mysql_num_rows($result);
                                                           
                                                                while($row = mysql_fetch_array($result)) {
                                                        
                                                        
                                                                 $name=$row['name'];
                                                       
                                                        //prints out restaurant name with link .....
                                                        echo "<a href=\"details.php?name=" .$name. "\"> $name</a>\n<br>";
                                                        
                                                        } 
                                                        
                    
                                                                    }
                                                                          

trying to have the links a-z on a page that will reload itself when a letter is clicked and display the appropriate records. easy one would think...

Link to comment
Share on other sites

I saw a few problems...but I still don't completely understand the code. Here it is with the top half rewritten the way I would have done it. Can you load it up with comments on what you are trying to accomplish exactly?

 

<?php
  $letter = isset($_GET['letter']) ? $_GET['letter'] : "A";
  $sql = "SELECT * FROM restaurants WHERE name LIKE '{$letter}%'";
  $result = mysql_query($sql) or die(mysql_error());
  while($row = mysql_fetch_assoc($result)){
    printf('<div align="left" width="100"><b>%s</b><br>%s</br>%s</br></div><hr color=#000 width=200></hr>',$row['name'],$row['address'],$result['cviolations']);
  }

  //alphabetical pagination links
  echo '<div align="center"><b>';
  foreach(range('A','Z') as $c){
    ($letter == $c)
      ? printf('%s&nbsp',$c)
      : printf('<a href="browse.php?letter=%s">%s </a>',$c,$c);
  }
  echo "</b></div><p>";

  //Not sure what the rest of this code does
  
  $sql="SELECT * FROM restaurants WHERE name LIKE '".$letter."%'";
  $query="SELECT * FROM songs WHERE UPPER(SUBSTRING(name,1,1)) NOT BETWEEN 'A' and 'Z'";

  $sql = "SELECT DISTINCT name FROM restaurants WHERE name LIKE '$letter.%' GROUP BY name DESC";
  $result = mysql_query($sql) or die(mysql_error()) ;
  $count = mysql_num_rows($result);

  while($row = mysql_fetch_array($result)) {
    $name=$row['name'];
    //prints out restaurant name with link .....
    echo "<a href=\"details.php?name=" .$name. "\"> $name</a>\n<br>";
  }
?>

Link to comment
Share on other sites

then just this code should work:

<?php
  $letter = isset($_GET['letter']) ? $_GET['letter'] : "A";

  //Show all restaurants that start with $letter
  $sql = "SELECT * FROM restaurants WHERE UCASE(name) LIKE '{$letter}%'";
  $result = mysql_query($sql) or die(mysql_error());
  while($row = mysql_fetch_assoc($result)){
    printf('<div align="left" width="100"><b>%s</b><br>%s</br>%s</br></div><hr color=#000 width=200></hr>',$row['name'],$row['address'],$result['cviolations']);
  }

  //alphabetical pagination links
  echo '<div align="center"><b>';
  foreach(range('A','Z') as $c){
    ($letter == $c)
      ? printf('%s&nbsp',$c)
      : printf('<a href="?letter=%s">%s</a> ',$c,$c);
  }
  echo "</b></div><p>";
?>

 

The first part should show all restaurants with the selected letter and the second part should show the letters. Are you getting any errors?

Link to comment
Share on other sites

what happens if you run this code:

<?php
  $letter = isset($_GET['letter']) ? $_GET['letter'] : "A";

  //Show all restaurants that start with $letter
  $sql = "SELECT * FROM restaurants";
  $result = mysql_query($sql) or die(mysql_error());
  while($row = mysql_fetch_assoc($result)){
    printf('<div align="left" width="100"><b>%s</b><br>%s</br>%s</br></div><hr color=#000 width=200></hr>',$row['name'],$row['address'],$result['cviolations']);
  }
?>

Link to comment
Share on other sites

I can't see anything wrong with this code:

 

<?php
  $letter = isset($_GET['letter']) ? $_GET['letter'] : "A";

  //Show all restaurants that start with $letter
  $sql = "SELECT * FROM restaurants WHERE UCASE(name) LIKE '{$letter}%'";
  $result = mysql_query($sql) or die(mysql_error());
  while($row = mysql_fetch_assoc($result)){
    printf('<div align="left" width="100"><b>%s</b><br>%s</br>%s</br></div><hr color=#000 width=200></hr>',$row['name'],$row['address'],$result['cviolations']);
  }

  //alphabetical pagination links
  echo '<div align="center"><b>';
  foreach(range('A','Z') as $c){
    ($letter == $c)
      ? printf('%s&nbsp',$c)
      : printf('<a href="?letter=%s">%s</a> ',$c,$c);
  }
  echo "</b></div><p>";
?>

Link to comment
Share on other sites

The only thing I can think of then is that the first character of the name is a blank character or something...what is the output of:

<?php
  $sql = "SELECT SUBSTR(first_name,1,1) let, count(*) cnt FROM `agents` GROUP BY let";
  $result = mysql_query($sql) or die(mysql_error());
  while($row = mysql_fetch_assoc($result)){
    print "{$row['let']}: {$row['cnt']}<br />";
  }
?>

 

Link to comment
Share on other sites

You were exactly right, there is a blank space getting stored infront of the names so I fixed that on the insert side and it works like a charm!! although I need help with the syntax on this part, trying to get the link to work for the view.php page...for each restaurant...keep getting the syntax error "unexpected T_Variable"

 printf('<a href="view.php?name=$row['name']"><b>%s</b><br>%s</br><br></a>',$row['name'],$row['address']);

Link to comment
Share on other sites

you may want to switch back to your print() function instead of printf(). i personally prefer printf() cus i think it looks cleaner. The basic jist of it, is you put a %s wherever you want the variable inserted, and then add the variables as arguments. so it would be like this:

 printf('<a href="view.php?name=%s"><b>%s</b><br>%s</br><br></a>',$row['name'],$row['name'],$row['address']);

Link to comment
Share on other sites

  • 1 month later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.