Click to See Complete Forum and Search --> : Echo results within script?


Jick
04-08-2004, 06:14 PM
Hey all...

I have a new question for whoever can help me. Ok, I'll cut right to the chase. I have a script which I will post below that connects to my database and retrieves all the rows of a specified table. Just to make this easier I will tell you what the script is intended for. This script gets announcements that I insert into the db by means of another page. I have no problem actually with any of the connecting or retrieving the info but I do have a problem with displaying it. I have been using br's to seperate each announcement down the page. I have it automaticly inserting br's into each announcement and then it all is outputed to the page. I have recently noticed that because of the way I'm doing it there is a gap left in the bottom of the last announcement because of the br's. I was told that if I could get the entire results (including the br's) into a var then I could use the function in php to cut the br's off the end of the last announcement getting rid of that annoying gap. My problem is that I don't know how I would get all the results into a var. Here is my script:<?php
mysql_connect("mysqlhost","mysqluser","mysqlpass");
mysql_select_db("mysqldb");
$result = mysql_query("SELECT * FROM news ORDER BY date DESC");
while($r = mysql_fetch_array($result))
{
$id = $r["id"]; $title = $r["title"]; $date = $r["date"]; $body = $r["body"];
echo "<b>$title</b> - <i>$date</i><br />$body<br /><br />\n";
}
?>Just so I don't get info I already have I will also post another version that I tried that didn't work:<?php
mysql_connect("mysqlhost","mysqluser","mysqlpass");
mysql_select_db("mysqldb");
$result = mysql_query("SELECT * FROM news ORDER BY date DESC");
while($r = mysql_fetch_array($result))
{
$id = $r["id"]; $title = $r["title"]; $date = $r["date"]; $body = $r["body"];
$var = "<b>$title</b> - <i>$date</i><br />$body<br /><br />\n";
$var = substr($var, 0, -12);
echo $var;
}
?>It didn't work because that way it was stripping the chars off of every row from the db which I don't want. I just want it to strip the chars from all of the rows as a whole. The reason I want to strip the chars from the very end is because that way it will remove the last couple br's thus removing the gap problem. I will be very greatful if someone can help me. :D

P.S. This is happining on my page (http://jick.zacknetwork.net/index.php) at the bottom where the announcements are displayed.

Jona
04-09-2004, 11:55 PM
I'd try something like this. Set a variable, like $j for example, to equal zero, before any of the code you posted (just before it, though), and then use something like this.


$id = $r["id"]; $title = $r["title"]; $date = $r["date"]; $body = $r["body"];
if(mysql_num_rows($result) == $j+1){
echo "<b>$title</b> - <i>$date</i><br />$body\n";
} else {
echo "<b>$title</b> - <i>$date</i><br />$body<br /><br />\n";
}
$j+=1;

Jick
04-10-2004, 02:28 AM
Ok, I gave what you provided a shot and it doesn't seem to have any effect. It is still displaying the same. Here is exactally what I have now so you can find out if I did it wrong or something:<?php
mysql_connect("mysqlhost","mysqluser","mysqlpass");
mysql_select_db("mysqldb");
$result = mysql_query("SELECT * FROM news_posts ORDER BY date DESC");
while($r = mysql_fetch_array($result)){
$j = 0;
$id = $r["id"]; $title = $r["title"]; $date = $r["date"]; $body = $r["body"];
if(mysql_num_rows($result) == $j+1){
echo "<b>$title</b> - <i>$date</i><br />$body\n";
} else {
echo "<b>$title</b> - <i>$date</i><br />$body<br /><br />\n";
}
$j+=1;
}
?>Thank you for your help. I really appreciate it as usual! :)

Jona
04-10-2004, 01:44 PM
I should have provided more code. Because you were setting the variable $j in the while loop, it was turning into zero every time - thus, having no effect whatsoever. The code should look like this:


$j = 0;
while($r = mysql_fetch_array($result)){
$id = $r["id"]; $title = $r["title"]; $date = $r["date"]; $body = $r["body"];
if(mysql_num_rows($result) == $j+1){
echo "<b>$title</b> - <i>$date</i><br />$body\n";
} else {
echo "<b>$title</b> - <i>$date</i><br />$body<br /><br />\n";
}
$j+=1;
}

Jick
04-10-2004, 03:58 PM
Ah, that did the trick! Thank you Jona. You've been a big help. That little bugger was stumpin me. :p

Jona
04-10-2004, 04:02 PM
Happy to help. :D

crh3675
04-10-2004, 08:03 PM
Simplified method:


while($r = mysql_fetch_array($result)){
$id = $r["id"]; $title = $r["title"]; $date = $r["date"]; $body = $r["body"];
$str.=$str!="" ? "<br/>":"";
$str.="<b>$title</b> - <i>$date</i><br/>$body\n";
}
print $str;