?
Solved

Can someone correct this piece of code?

Posted on 2003-02-20
14
Medium Priority
?
259 Views
Last Modified: 2013-12-13
Can someone tell me why the following code produce wrong output giving me the fields two times per record?<?php
mysql_pconnect("","","");
mysql_select_db("myfirstdb");

$c1 = "#FFCCCC";   # color1
$c2 = "#FFEEEE";   # color2
$c  = 0;           # flag

$result = mysql_query("select lastname, firstname, birthdate from names order by lastname, firstname");

echo "<table border=1>";
while($r=mysql_fetch_array($result))
{    
echo "<tr bgcolor=".(($c++&1)?$c1:$c2)."><td>";
echo implode("</td><td>",$r);
echo "</td></tr>";

}
echo "</table>";
?>


my output is like this

dimitrios dimitrios pappas pappas 2014-11-10 2014-11-10
dimitris dimitris patelis patelis 2015-12-10 2015-12-10
kapatos kapatos karmiris karmiris 2000-12-08 2000-12-08
kariotoglou kariotoglou kostas kostas 2012-12-20 2012-12-20
kariotoglou kariotoglou kostas kostas 2012-12-20 2012-12-20
kariotoglou kariotoglou tataras tataras 2013-09-08 2013-09-08
karras karras kostas kostas 2012-12-20 2012-12-20
karras karras kostas kostas 1900-09-06 1900-09-06

all fields are showing two times.!!!
why?

0
Comment
Question by:vage78
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 3
  • +1
14 Comments
 
LVL 7

Expert Comment

by:Big_Red_Dog
ID: 7987072
The fetch method you are using is returning an array in two different forms in the same array.
i.e. The first element in $r can either be accessed through $r[0] or $r['fieldname']

To fix this change
mysql_fetch_array
to
mysql_fetch_row
or
mysql_fetch_assoc
0
 

Author Comment

by:vage78
ID: 7991900
Can you please give me the full code?
0
 
LVL 1

Expert Comment

by:Jonza
ID: 7991982
?<?php
mysql_pconnect("","","");
mysql_select_db("myfirstdb");

$c1 = "#FFCCCC";   # color1
$c2 = "#FFEEEE";   # color2
$c  = 0;           # flag

$result = mysql_query("select lastname, firstname, birthdate from names order by lastname, firstname");

echo "<table border=1>";
while($r=mysql_fetch_assoc($result))
{    
echo "<tr bgcolor=".(($c++&1)?$c1:$c2)."><td>";
echo $r[lastname]." - ".$r[firstname]." - "$r[birthdate];
echo "</td></tr>";

}
echo "</table>";
?>



try that


0
Video: Liquid Web Managed WordPress Comparisons

If you run run a WordPress, you understand the potential headaches you may face when updating your plugins and themes. Do you choose to update on the fly and risk taking down your site; or do you set up a staging, keep it in sync with your live site and use that to test updates?

 

Author Comment

by:vage78
ID: 7992083
Hi it gives me an error in this line


echo $r[lastname]." - ".$r[firstname]." - "$r[birthdate];

0
 
LVL 22

Expert Comment

by:rascalpants
ID: 7994696
there is a missing "." before $r[birthdate];



but shouldn't this line:

echo $r[lastname]." - ".$r[firstname]." - "$r[birthdate];


be:

echo "$r[lastname]. - .$r[firstname]. - .$r[birthdate]";


but I am fairly new to PHP...


rp
0
 
LVL 7

Expert Comment

by:Big_Red_Dog
ID: 7995892
Thank you.  The email notifications to me were broken.  EE has fixed them, so I missed handing you the code fragment.  I hope it works for you!
0
 

Author Comment

by:vage78
ID: 8023477
Hi rascapants
It worked finally. But the fields are separated by "-".
How could be separated by verical lines. Like a simple table with borders verical. Like a table in word.
thanks
0
 
LVL 22

Expert Comment

by:rascalpants
ID: 8025843
maybe something like this:

echo "<html><body><table width=300 border=1><tr><td width=100>$r[lastname]</td><td width=100>$r[firstname]</td><td width=100>$r[birthdate]</td></tr></table></body></html>";


this just uses HTML and a simple table...

is that what you wanted?


rp
0
 
LVL 1

Expert Comment

by:Jonza
ID: 8031364
echo $r[lastname]." - ".$r[firstname]." - ".$r[birthdate];
 
with vertical lines

echo $r[lastname]." | ".$r[firstname]." | ".$r[birthdate];

0
 

Author Comment

by:vage78
ID: 8031504
Is not exactly what I want. I want to make a table like tables in MS Word. With horizontal and vertical lines where
at every cell be a field of my table.
Thanks
0
 
LVL 1

Expert Comment

by:Jonza
ID: 8031521
well try this, i didn't test this so tell me if there is something wrong


-----------------------------
<?php
mysql_pconnect("","","");
mysql_select_db("myfirstdb");

$c1 = "#FFCCCC";   # color1
$c2 = "#FFEEEE";   # color2
$c  = 0;           # flag

$result = mysql_query("select lastname, firstname, birthdate from names order by lastname, firstname");

echo "<table border=1 cellpadding='4' cellspacing='1'>";
while($r=mysql_fetch_assoc($result))
{    
     echo "<tr bgcolor=".(($c++&1)?$c1:$c2).">";
     foreach($r as $value){
          echo "<td>".$value."</td>";
     }
     echo "</tr>";

}
echo "</table>";
?>
----------------------------------------
0
 
LVL 7

Expert Comment

by:Big_Red_Dog
ID: 8031757
Sorry I've been out.  When you use mysql_fetch_assoc() and you don't want the two result types, you must specify, in your case, MYSQL_ASSOC as a second parameter.

<?php
mysql_pconnect("","","");
mysql_select_db("myfirstdb");

$c1 = "#FFCCCC";   # color1
$c2 = "#FFEEEE";   # color2
$c  = 0;           # flag

$result = mysql_query("select lastname, firstname, birthdate from names order by lastname, firstname");

echo "<table border=1 cellpadding='4' cellspacing='1'>";
while($r=mysql_fetch_assoc($result, MYSQL_ASSOC))
{    
    echo "<tr bgcolor=".(($c++&1)?$c1:$c2).">";
    foreach($r as $value){
         echo "<td>".$value."</td>";
    }
    echo "</tr>";

}
echo "</table>";
?>
0
 
LVL 22

Accepted Solution

by:
rascalpants earned 100 total points
ID: 8034525
vage78,

I gave you exactly what you wanted...  you needed your information to be in cells... that is what I gave you...  an html table that has your data in each cell...


use the code I posted above(before anyone else) and add a couple of tweaks to make it look how you want...  do you need help with the tweaking?


echo "<html><body><table width=300 border=1><tr><td width=100>$r[lastname]</td><td width=100>$r[firstname]</td><td width=100>$r[birthdate]</td></tr></table></body></html>";


this gives you what you wanted...

rp
0
 

Author Comment

by:vage78
ID: 8041238
Thanks for your help
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question