...

View Full Version : PHP & mySQL loop and formatting *help*



JayStang
01-08-2006, 08:10 PM
Hi guys,

Basically what i'm doing is making a website for a band. For the news section i'm trying to just make it so the actual band members can go in and add new news content with out having to contact me every time they want new news posted. So i'm just making a simple form which saves to a mysql database and then is pulled from the database and viewed in tables.

I have it working so it views the latest entry but it wont show all of them... currently i have 3 entries in the database. I don't know how to make it so it will create a new table for the number of entries and display them all.

Here is the code i came up with:


<body>
<?
$host = 'localhost';
$user = 'username';
$pass = 'password';
$db = 'ctechnow_news';

$m_connect = @mysql_connect($host,$user,$pass) or die ("Error while trying to connect to mysql: " . mysql_error());

$select_my_db = @mysql_select_db($db,$m_connect) or die ("Error selecting database: " . mysql_error());

$query="SELECT * FROM tblNews";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

$i=0;
while ($i < $num) {

$title=mysql_result($result,$i,"fldTitle");
$message=mysql_result($result,$i,"fldMessage");

$i++;
}

?>

<table width="708" height="97" border="0">
<tr>
<td width="46"><div align="right"><span class="style1">Topic:</span></div></td>
<td width="652"><? echo $title; ?></td>
</tr>
<tr>
<td><div align="right"><span class="style1">Date:</span></div></td>
<td>PHP Date</td>
</tr>
<tr>
<td><div align="right"><span class="style1">Message:</span></div></td>
<td><? echo $message; ?></td>
</tr>

<?
$i++;
?>

</table>
<p>&nbsp;</p>
</body>

(ignore the date portion i havn't worked on that yet)

you can post comment from here:
http://ctechnow.com/jdulude/test/mysql_login/post_news.html

and view the output here:
http://ctechnow.com/jdulude/test/mysql_login/news_table.php


also my other question is once that problem is resolved and it shows multiple tables, is there a way to make every other post a different color. I was thinking maybe an if statement could do it. The database has a 'fldID' which autonumbers. So maybe something like when the odd fldID numbers are displayed it will change the background color of the table.

Any help would be great,
Thanks,
~Jamie

Element
01-08-2006, 08:23 PM
mysql_num_rows() is the correct function. Yes, I've seen this tutorial that has the typo with no underscore, so its just a mistake.

Here is some more fixes.



<body>
<?
$host = 'localhost';
$user = 'username';
$pass = 'password';
$db = 'ctechnow_news';

$m_connect = @mysql_connect($host,$user,$pass) or die ("Error while trying to connect to mysql: " . mysql_error());

$select_my_db = @mysql_select_db($db,$m_connect) or die ("Error selecting database: " . mysql_error());

$query="SELECT * FROM tblNews";
$result=mysql_query($query);

$num=mysql_num_rows($result);

mysql_close();

$i=0;
while ($i < $num) {

$title=mysql_result($result,$i,"fldTitle");
$message=mysql_result($result,$i,"fldMessage");

?>

<table width="708" height="97" border="0">
<tr>
<td width="46"><div align="right"><span class="style1">Topic:</span></div></td>
<td width="652"><? echo $title; ?></td>
</tr>
<tr>
<td><div align="right"><span class="style1">Date:</span></div></td>
<td>PHP Date</td>
</tr>
<tr>
<td><div align="right"><span class="style1">Message:</span></div></td>
<td><? echo $message; ?></td>
</tr>

<?

$i++;

}

?>

</table>
<p>&nbsp;</p>
</body>

JayStang
01-08-2006, 08:57 PM
that worked great... displays all the posts now. does any one know how to change the background colors which i explained above?

Also how would i make it so displays the posts in reverse order? So that the newest post is on top?

Thanks,
~Jamie

Velox Letum
01-08-2006, 09:10 PM
$query="SELECT * FROM tblNews ORDER BY date DESC";

Date would be the time field you want to sort by. It might be 'posttime' or whatever.

JayStang
01-09-2006, 04:05 AM
Can someone look at my if else statement to see if i'm coming even close? This is for trying to change the bg color of every other table. Right now i set the variable "odd" to 3 just to test so it should only change the color of the table displaying fldID 3. You can see in the table where i'm trying to slip my the variable bg_color to try and change the table

Thanks.



<body>
<table width="200" border="0" align="center">
<tr>
<td><div align="center">H2S News </div></td>
</tr>
</table>
<p>&nbsp;</p>

<?
$host = 'localhost';
$user = 'username';
$pass = 'password';
$db = 'ctechnow_news';

$m_connect = @mysql_connect($host,$user,$pass) or die ("Error while trying to connect to mysql: " . mysql_error());

$select_my_db = @mysql_select_db($db,$m_connect) or die ("Error selecting database: " . mysql_error());

$query="SELECT * FROM tblNews ORDER BY fldDate DESC";
$result=mysql_query($query);

$num=mysql_num_rows($result);

mysql_close();

$i=0;
while ($i < $num) {

$title=mysql_result($result,$i,"fldTitle");
$date=mysql_result($result,$i,"fldDate");
$show_date=date("h:ia m/d/Y", $date);
$message=mysql_result($result,$i,"fldMessage");


?>
<?
//IF-Else Statement for changing table color

$id=mysql_result($result,$i,"fldID");
$odd=3;


if ($id==$odd){
$bg_color = "83090C";
}
?>


<table width="708" height="97" border="0" bgcolor=<? $bg_color ?>>
<tr>
<td width="46"><div align="right"><span class="style1">Topic:</span></div></td>
<td width="652"><? echo $title; ?></td>
</tr>
<tr>
<td><div align="right"><span class="style1">Date:</span></div></td>
<td><? echo $show_date; ?></td>
</tr>
<tr>
<td><div align="right"><span class="style1">Message:</span></div></td>
<td><? echo $message; ?></td>
</tr>
<br />
<hr />

<?
$i++;
}
?>

</table>

<p>&nbsp;</p>
</body>

ralph l mayo
01-09-2006, 04:35 AM
You can use something like this:



// somewhere outside the while loop
$bgcolors = array('evencolor', 'oddcolor');

// then, inside, wherever you specify the color use
echo $bgcolors[$i%2];


% is the modulus operator, used to determine the remainder after division. Modulus by 2 returns 0 for even numbers and 1 for odd numbers, so it will alternate the index of the $bgcolors array.

edit for bonus limited edition bitwise method:


echo $bgcolors[1&$i];

firepages
01-09-2006, 05:31 AM
mysql_num_rows() is the correct function. Yes, I've seen this tutorial that has the typo with no underscore, so its just a mistake.

mysql_numrows() is a deprecated function not a typo, I mean it IS wrong cos its deprecated, but I still sadly see lots of code that uses it.

JayStang
01-09-2006, 05:41 AM
Ralf... thank you for the help... that worked perfectly!............in Internet Explorer.

When i tried it in firefox it doesn't display any background color at all. Its just plan solid background all the way through.

Check it out
http://ctechnow.com/jdulude/test/mysql_login/news_table_color.php

i have no idea why its doing this. This is the code that i'm using after your help.


<body>
<table width="200" border="0" align="center">
<tr>
<td><div align="center">H2S News </div></td>
</tr>
</table>
<p>&nbsp;</p>

<?
$host = 'localhost';
$user = 'ctechnow';
$pass = 'ctechn2';
$db = 'ctechnow_news';

$m_connect = @mysql_connect($host,$user,$pass) or die ("Error while trying to connect to mysql: " . mysql_error());

$select_my_db = @mysql_select_db($db,$m_connect) or die ("Error selecting database: " . mysql_error());

$query="SELECT * FROM tblNews ORDER BY fldDate DESC";
$result=mysql_query($query);

$num=mysql_num_rows($result);

mysql_close();



//Statement for changing table color

$id=mysql_result($result,$i,"fldID");


$bgcolors = array('b9b989', '83090C');



$i=0;
while ($i < $num) {

$title=mysql_result($result,$i,"fldTitle");
$date=mysql_result($result,$i,"fldDate");
$show_date=date("h:ia m/d/Y", $date);
$message=mysql_result($result,$i,"fldMessage");


?>


<table width="708" height="97" border="0" align="center" bgcolor=<? echo $bgcolors[$i%2]; ?>>
<tr>
<td width="46" valign="top"><div align="right"><span class="style1">Topic:</span></div></td>
<td width="652"><? echo $title; ?></td>
</tr>
<tr>
<td valign="top"><div align="right"><span class="style1">Date:</span></div></td>
<td><? echo $show_date; ?></td>
</tr>
<tr>
<td valign="top"><div align="right"><span class="style1">Message:</span></div></td>
<td><? echo $message; ?></td>
</tr>

<?
$i++;
}
?>

</table>

<p>&nbsp;</p>
</body>


anyone know why it wouldn't display in the table bg colors in firefox and how to fix it?

ralph l mayo
01-09-2006, 05:52 AM
$bgcolors = array('b9b989', '83090C');
...
bgcolor=<? echo $bgcolors[$i%2]; ?>>


bgcolor is deprecated, replace the relevant code with something like:
style="background:<? echo $bgcolors[$i%2]; ?>;"

Note the attributes are surrounded by quotation marks with a trailing semicolon.
Also, preface your color codes with a hash mark, like '#b9b989'

JayStang
01-09-2006, 06:05 AM
perfect!... all it needed was the '#' in front of it... thank you for all the help!!

~Jamie

mcoelho123
05-19-2006, 04:30 PM
I just try this:

<?php $bgcolors = array('#b9b989', '#83090C'); ?>
<?php do { ?>
<tr bgcolor=<php echo $bgcolors[$i%2]; ?>>
<td width="15" class="bodytext11_blue"><div align="left"><?php echo $row_Recordset1['T&#237;tulo Honor&#237;fico']; ?></div></td>
<td width="300" class="bodytext11_blue"><div align="left"><?php echo $row_Recordset1['Nome']; ?></div></td>
<td width="180" class="bodytext11_blue">

but it shows the same color, can anyone discover whats wrong?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum