PHP help

HunterSeeker

Newbie
Joined
Apr 13, 2004
Messages
1,694
Reaction score
0
I think this is the right forum...

Anyway I need some help witth a website I am working on, currently I am working on a news system and it is supposed to display the 3 lateest news but it only displays the absolutly latest.

Please, take a look to see if you can find the problem:

Code:
<?php
$i=0;
while($i!=3){

//Läs in rubriken

$db=mysql_connect("localhost","pierre","870430");
mysql_select_db("Pierre",$db);
$result = mysql_query("SELECT Header FROM Nyheter ORDER BY Datum DESC",$db);
$getrow = mysql_fetch_row($result);
if (strlen($getrow[$i]) >= 3)
{
echo "<h1>" . $getrow[$i]. "</h1>";
//echo $i; Debugger...
//Läst in datumet

$db=mysql_connect("localhost","pierre","870430");
mysql_select_db("Pierre",$db);
$result = mysql_query("SELECT Datum FROM Nyheter ORDER BY Datum DESC",$db);
$getrow = mysql_fetch_row($result);
echo "<h2> Skapad: " . $getrow[$i]. "</h2>";


//Läst in texten

$db=mysql_connect("localhost","pierre","870430");
mysql_select_db("Pierre",$db);
$result = mysql_query("SELECT Textbody FROM Nyheter ORDER BY Datum DESC",$db);
$getrow = mysql_fetch_row($result);
echo "[br]" . $getrow[$i] . "[br][br]";
//Avsluta ifen
}
$i++;
}
?>

By the way, before anyone mentions it:
For loops = :devil:
 
Wouldn't it be easier to do a single query and to put LIMIT 0,3 at the end? That's how I'd do it, but I see that the way I code it is quite a bit different from your style. I'd just use this:

PHP:
<?php
	$db=mysql_connect("localhost","pierre","870430");
	mysql_select_db("Pierre",$db);

	$query="SELECT Header, Datum, Textbody FROM Nyheter ORDER BY Datum DESC LIMIT 0,3";
	$result=mysql_query($query);
	$num=mysql_numrows($result); 
									
	$i=0;
	while ($i < $num) {
		$Header=mysql_result($result,$i,"Header");
		$Datum=mysql_result($result,$i,"Datum");
		$Textbody=mysql_result($result,$i,"Textbody");

											
		print ('Bla bla bla.  Print out your content here');
		++$i;
	} 
?>

I'm not completely sure that's the most efficient way, but it should work.
 
It's alot faster with the SQL LIMIT statement.

I would personnaly do it like this:

PHP:
<?php
    $db=mysql_connect("localhost","pierre","870430");
    mysql_select_db("Pierre",$db);

    $MaxNews = 3;
    $query="SELECT Header, Datum, Textbody FROM Nyheter ORDER BY Datum DESC LIMIT 0,".$MaxNews;
    $result=mysql_query($query);
                                    
    while ($row = mysql_fetch_object($result)){
        $Header    = $row->Header;
        $Datum     = $row->Datum;
        $Textbody = $row->Textbody;

        echo '
            <table>
               <tr>
                 <td>'.$Header.' - '.$Datum.'</td>
              </tr>
               <tr>
                 <td>'.$Textbody.'</td>
              </tr>
            </table>';

        echo '[br]';
    }
?>
 
Back
Top