Page 1 of 1

Help please

Posted: Sun Sep 26, 2010 7:59 am
by Portnumbay
Hi all..
I need help.. Hope someone can help me ...

example I have a table where the data are :

- TV
- Handphone
- Book

this is my script

Code: Select all

$slq=mysql_query("SELECT * FROM...);
            
            while ($result=mysql_fetch_array($sql)){

echo $result['name']; <br />

}  
The result gonna be :
TV
Handphone
Book

But i want my data displayed like this one :

TV
=========
Handphone
=========
Book

The last data (book) won't display '======'
I tried to use if like this one :

Code: Select all

$totaldata = mysql_num_rows($sql);
$no = 0;
while ($result=mysql_fetch_array($sql)){

echo $result['name']; <br />
if ($no != $totaldata)
{
echo " ";
}
else 
{
echo "======"
}
$no++;
}  
But it didn't work.. can somebody help me please ?
Thanks

Re: Help please

Posted: Sun Sep 26, 2010 5:20 pm
by JakeJ
If the goal is just to print ======== between each line, you've got extraneous code. Shorten it to this. I couldn't really see the purpose of $no and $totaldata in the context of what you were trying to accomplish.

Code: Select all

while ($result=mysql_fetch_array($sql)){
echo $result['name']; <br />
echo "======"
} 

Re: Help please

Posted: Sun Sep 26, 2010 6:37 pm
by buckit
JakeJ wrote:If the goal is just to print ======== between each line, you've got extraneous code. Shorten it to this. I couldn't really see the purpose of $no and $totaldata in the context of what you were trying to accomplish.

Code: Select all

while ($result=mysql_fetch_array($sql)){
echo $result['name']; <br />
echo "======"
} 
based on what he typed to show how he wants it displayed... he doesnt want a "======" below the last element. your code puts it at the bottom of every elemnt. that was the purpose of is if statement, but he didnt execute it very well.

personally, I would think of it a little backwards. rather than put ====== BELOW each element except for the last, I would put it at the top of each one except for the first.

something like this:

Code: Select all

$no = 0;
while ($result=mysql_fetch_array($sql)){
if($no > 0){ echo "======";}
echo $result['name']; <br />
$no++;
} 

if you wanted to use JakeJ's suggestion then you could put everything into a variable, then remove the final ======.

like this:

Code: Select all

while ($result=mysql_fetch_array($sql)){
$theCode = $result['name']."<br /> ======";
} 
echo substr($theCode, 0, -6);

Re: Help please

Posted: Wed Sep 29, 2010 11:56 pm
by Portnumbay
buckit wrote:
JakeJ wrote:If the goal is just to print ======== between each line, you've got extraneous code. Shorten it to this. I couldn't really see the purpose of $no and $totaldata in the context of what you were trying to accomplish.

Code: Select all

while ($result=mysql_fetch_array($sql)){
echo $result['name']; <br />
echo "======"
} 
based on what he typed to show how he wants it displayed... he doesnt want a "======" below the last element. your code puts it at the bottom of every elemnt. that was the purpose of is if statement, but he didnt execute it very well.

personally, I would think of it a little backwards. rather than put ====== BELOW each element except for the last, I would put it at the top of each one except for the first.

something like this:

Code: Select all

$no = 0;
while ($result=mysql_fetch_array($sql)){
if($no > 0){ echo "======";}
echo $result['name']; <br />
$no++;
} 

if you wanted to use JakeJ's suggestion then you could put everything into a variable, then remove the final ======.

like this:

Code: Select all

while ($result=mysql_fetch_array($sql)){
$theCode = $result['name']."<br /> ======";
} 
echo substr($theCode, 0, -6);
Well finally i figured it out... still using the same condition.. i tried

Code: Select all

<?
            if ($no < $totalRows)
            {
            echo"========";
            }
            
            ?>
            <?
            $border++;
            }
            
             ?>
from this syntax... it will keep print "=====" as long as the $no is < $totalrows ..
Thanks anyway for all the suggestions...