Jump to content

How to sort a list after so many things from a Table.


Ultra

Recommended Posts

I was wondering if it was possible to get something from a Database [$result102 = mysql_query("select * from $table WHERE Owner_Id= '$iD' AND Slot= 'Box' order by Name asc ", $link)], use a mysql fetch array [while ($qry = mysql_fetch_array($result102))], post them with the $qry variable, and have it so that after 5 posts, it makes a new table row. I'm trying to make a list, and submit 5 things from the DB, then have it set a new table row so it doesn't mess up the screen.

Link to comment
Share on other sites

Er...

Can you/someone explain to me what he/she means? I'm no coding expert, started not too long ago. Heck, I don't get what you mean by: 'Set a counter' and 'When the counter is at 5, reset the counter, end the current row and then start a new one.'. I only get the loop part, but just barely. ._.

 

Edit: Now that I look at it and think, I actually sort of get it. Would I have to set up a variable, and an if statement, saying: 'if($counter=5) $counter = 0', or something similar?

Edited by Ultra
Link to comment
Share on other sites

Work through this example from a PHP book by Larry Ullman that prints out 5 cities and the zip codes.

 

>br />        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">



Cities and Zip Codes
<br />h2 {<br />    font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; <br />    font-size: 14pt;  <br />    color : #960;<br />    text-align: center;<br />}<br />td {<br />    font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; <br />    font-size: 10pt;  <br />    color : #333<br />}<br />.error {<br />    color: #F30;<br />}<br />



/*    This page retrieves and displays all of the
*    cities and zip codes for a particular state.
*    The results will be shown in a table.
*/

// Abbreviation of state to show:
$state = 'AK';

// Items to display per row:
$items = 5;

// Print a caption:
echo "Cities and Zip Codes found in $state\n";

// Connect to the database:
$dbc = @mysqli_connect ('localhost', 'username', 'password', 'zips') OR die ('
Cannot connect to the database.');

// Get the cities and zip codes, ordered by city:
$q = "SELECT city, zip_code FROM zip_codes WHERE state='$state' ORDER BY city";
$r = mysqli_query($dbc, $q);

// Retrieve the results:
if (mysqli_num_rows($r) > 0) {

   // Start a table:
   echo '</pre>
<table border="2" width="90%" cellspacing="3" cellpadding="3" align="center">
';

   // Need a counter:
   $i = 0;

   // Retrieve each record:
   while (list($city, $zip_code) = mysqli_fetch_array($r, MYSQLI_NUM)) {

       // Do we need to start a new row?
       if ($i == 0) {
           echo "\n";
       }

       // Print the record:
       echo "\t$city, $zip_code\n";

       // Increment the counter:
       $i++;

       // Do we need to end the row?
       if ($i == $items) {
           echo "\n";
           $i = 0; // Reset counter.
       }

   } // End of while loop.

   if ($i > 0) { // Last row was incomplete.

       // Print the necessary number of cells:
       for (;$i             echo " \n";
       }

       // Complete the row.
       echo '';

   } // End of ($i > 0) IF.

   // Close the table:
   echo '</table>';<br><br>} else { // Bad state abbreviation.<br><br>   echo '<p class="error">An invalid state abbreviation was used.</p>';<br><br>} // End of main IF.<br><br>// Close the database connection:<br>mysqli_close($dbc);<br><br>?><br><br><p

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...