...

View Full Version : I get one entries when I want the last 5



Goran
03-18-2009, 06:19 PM
Hi, I need some help. My code works and I get the last input. Now what to do if I want the 5 last inputs?



<?PHP

//inställningar-----------------------------------------------------------------------------

$antal_dagar = 1; //Hur många dagar ska meddelandet visas
$reklamtext = "Inga bud ännu!";

//------------------------------------------------------------------------------------------



$dbh=mysql_connect ("localhost", "xxxx","xxxxx") or die('Cannot connect to the database because: ' . mysql_error());
mysql_select_db ("xxxxxx");



$result = mysql_query("SELECT caller FROM samtal ORDER BY id DESC LIMIT 10");

$r = mysql_fetch_row($result);
if($r[0]=="")
print $reklamtext;
else
print $r[0];

?>

PappaJohn
03-18-2009, 06:39 PM
You will have to loop through the results from your query. ie:

Instead of:


$result = mysql_query("SELECT caller FROM samtal ORDER BY id DESC LIMIT 10");

$r = mysql_fetch_row($result);
if($r[0]=="")
print $reklamtext;
else
print $r[0];

?>



try


$result = mysql_query("SELECT caller FROM samtal ORDER BY id DESC LIMIT 10");

while ($r = mysql_fetch_row($result))
{
if($r[0]=="")
print $reklamtext;
else
print $r[0];
}

Goran
03-18-2009, 06:53 PM
Thank you so very much, Now I have 5 but I get them in a line. I want to see them like

123123
123123
123123
123123
123123

Goran
also called PappaGoran ;)

steelaz
03-18-2009, 07:23 PM
Just add line break after printing each result:



while ($r = mysql_fetch_row($result))
{
if($r[0]=="")
print $reklamtext;
else
print $r[0] .'<br>';
}

Goran
03-18-2009, 07:58 PM
Great. Thank you. One more thing. I How to do if I now only want to show
the last 5 figures in every line like:

xxxx23123
xxxx23123
xxxx23123
xxxx23123
xxxx23123

Goran

steelaz
03-18-2009, 08:15 PM
To get last characters of the string you can use substr() (http://www.php.net/substr) function:


print substr($r[0], -5, 5) .'<br>';

Goran
03-18-2009, 08:22 PM
Thank you. It made my day ;)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum