Forum Moderators: coopster

Message Too Old, No Replies

Order list or Numbering Index

order list

         

chrissim

2:32 pm on May 2, 2010 (gmt 0)

10+ Year Member



hi guys

Need some help here... Ok i have managed to pull out data Randomly from my mysql database table alright and displaying them in 5 columns but unfortunately there was no order list such as 1,2,3,4,5 etc. I tried to use ol list code but it came out funny like this below

1. site
1. site
1. site
etc

instead of

1. site
2. site
3. site

So guys here's my code below and hope someone could brief me the proper coding here. Thanks...


Print "<table width='935' cellpadding='1' cellspacing='0' border='0' align='center'>";
$count = 0;
$columns = 5; //column number declaration
if(mysql_num_rows($result)){
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
// display list if there are records to display
if($count == 0)
Print "<tr>";
echo '<td bgcolor="#000000">';

echo '<b><a href="http://'.$row['website'].'" target="_blank">New Site</a></b>';

if($count == $columns - 1)
Print "</tr>";//here without the!
$count = ++$count % $columns;
}
if($count!= 0)
Print "</tr>";
Print "</table>";

rocknbil

8:38 pm on May 2, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here is your problem,

$count = ++$count % $columns;

modulus returns the result left over when divided by $count so 1 / 5 = .20. Then you increment the result without a specific number which is an integer value, and since .20 is less than .5, it's like incrementing zero, so it will always be 1.

Try this . . . it's a better row alternating method, especially for tables. If your row data doesn't always divide by 5, you will have a messed up last row with some cells missing. This **may** appear OK visually in some browsers . . . . but may not, and will still be invalid html.



echo "<table width='935' cellpadding='1' cellspacing='0' border='0' align='center'>";
$count = 0;
$columns = 5; //column number declaration
$light = 'style="background:#fff"';
$tint = 'style="background:#fbfbfb';

//
if(mysql_num_rows($result)){
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
// display list if there are records to display
//
$bg = ($bg==$tint)?$light:$tint;
if($count == 0) { Print "<tr>"; }
echo "<td $bg>";
//
echo '<b><a href="http://'.$row['website'].'" target="_blank">New Site</a></b>';
//
$count++;
if ($count >= $columns) { echo "</tr>"; $count= 0; }

}
}
// Last row fix
if (($count > 0) and ($count < $columns)) {
for ($i=$count;$i<$columns;$i++) {
echo "<td $bg>&nbsp;</td>";
echo "</tr>";
}
}
echo "</table>";

chrissim

12:12 am on May 3, 2010 (gmt 0)

10+ Year Member



hi rocknbil

Thanks but i tried your coding there and it came out blank page
Please advise mike

chrissim

12:49 am on May 3, 2010 (gmt 0)

10+ Year Member



hi rocknbil

Sorry your coding are fine here ...i made some mistake here of my script.
But it but i applied order list such as <ol><li><b><a href="http://'.$row['website'].'" target="_blank">New Site</a></b></li></ol> it still gave me

1. site
1. site
1. site

Please help..

Readie

12:56 pm on May 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm going to approach this differently.

EDIT:

Oopsie, didn't fully read your post.
if($rows = mysql_num_rows($result)) {
echo '<table>' .
"\n\t" . '<tr>' .
"\n\t\t" . '<td>Col 1</td>' .
"\n\t\t" . '<td>Col 2</td>' .
"\n\t\t" . '<td>Col 3</td>' .
"\n\t\t" . '<td>Col 4</td>' .
"\n\t\t" . '<td>Col 5</td>';
for($i = 0; $i < $rows; $i++) {
if($n = ($i % 5)) {
echo "\n\t\t" . '<td><b>' . ($i + 1) . '. <a target="_blank" href="http://' . mysql_result($result, $i, "website") . '">New site</a></b></td>'
} else {
echo "\n\t" . '</tr>' .
"\n\t" . '<tr>' .
"\n\t\t" . '<td><b>' . ($i + 1) . '. <a target="_blank" href="http://' . mysql_result($result, $i, "website") . '">New site</a></b></td>';
}
}
if($n) {
for($i = $n; $i < 5; $i++) {
echo "\n\t\t" . '<td>&nbsp;</td>';
}
}
echo "\n\t" . '</tr>' . "\n" . '</table>';
}

Readie

2:16 pm on May 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Looking back on this, I think I made a mistake. If the above goes all mangled, try replacing this line:
if($n = ($i % 5)) {

With this one:
if($n = (($i + 1) % 5)) {

chrissim

3:01 pm on May 3, 2010 (gmt 0)

10+ Year Member



hi Readie

Sorry but when i tried to run your coding i got only blank page.
Any idea here?

Thanks

Readie

3:26 pm on May 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do you have something like this (though relative to your database layout ofcourse)?

$sql = 'SELECT * FROM website_table';
$result = mysql_query($sql);


before the code you copied/pasted from above?

rocknbil

5:15 pm on May 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Chrisim: If you check your error log, you will see I have an extra closing bracket (I think?) A blank page means your server is likely configured to not output PHP errors, which is often a good thing. I trying to assist, I am just providing logic, code is not always "working code." Look . . .

if(mysql_num_rows($result)){
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
// cut
}
} // OOPS, we don't want to end IF here, remove
// Last row fix
if (($count > 0) and ($count < $columns)) {
// cut
}
} // This one should be right

You know what they say . . . "give a man a fish . . . he eats for a day . . . "

CyBerAliEn

7:50 pm on May 3, 2010 (gmt 0)

10+ Year Member



I'm not fully involved in exactly what is going on here... but I did catch something I thought I'd comment on:

Some servers (for security) will NOT output PHP errors if they occur. For simple errors like E_NOTICE, the errors will usually just be hidden and the page will load. But for something more like a warning or fatal error, PHP will stop and nothing will get outputted. It then leaves you (the developer) wondering "what happened?"

You can force the server to show PHP errors via your script using the code below. Put it at the VERY TOP of your script:

error_reporting(E_ALL);
ini_set('display_errors', '1');


This should work; but it may not depending on your server settings. I usually add this code to whatever I'm working on that is in development (to see all errors). But you shouldn't keep it on production (public) level work.


(this advice may help you see any specific PHP errors that may be occurring if your server has such errors disabled)

Readie

6:46 pm on May 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



error_reporting(E_ALL);
ini_set('display_errors', '1');

Just to share, the above will fail if you have a parse error (i.e. syntax error) as the script will terminate before the ini_set() function can be executed.

I had this problem when we went live with a shared server that I was using as a development server and display_errors was turned off in the php.ini file.

Really simple fix though, just add the following line to your .htaccess file:

php_flag display_errors on

Remember to remove it or comment it out before you go live with your site, however.