Hey I know how to select data from the db using PHP, but when i do, it comes out like this Field name[1] fieldvalue[1] Field name[1] fieldvalue[1] What i want is Fieldname[1] Fieldname[1] Fieldvalue[1] Fieldvalue[1] Fieldvalue[2] Fieldvalue[2] ect. Any ideas....
I got a complicated way mysql_connect ("localhost", "root"); mysql_select_db(pet_shop); $query = "SELECT product, total_price FROM cust WHERE cust_id = '".$cust_num."'"; $result = mysql_query($query); echo "<table border='1'> <thead> <tr>"; for($i = 0;$i < mysql_num_fields($result);$i++) { echo "<th>".mysql_field_name($result,$i). "</th>"; } echo " </tr> </thead> <tbody>"; for ($i = 0; $i < mysql_num_rows($result); $i++) { echo "<tr>"; $row = mysql_fetch_row($result); for($j = 0;$j<mysql_num_fields($result);$j++) { echo("<td>" . $row[$j] . "</td>"); } echo "</tr>"; } echo "</tbody></table>"; PHP: Better ideas are welcome
Just do 1 loop and build your header into one variable and your rows into another then echo them both at the end.
you can use while loop instead of for loop reducing the number of calls made to functions eg. while($row=mysql_fetch_row($result)){ . //your code here . }
This may be the only acceptable method of using <table> I've ever run across. I'd have to see your code to tell you how to fix the problem. It's not a big deal. But, without your code, it's a guessing game. This is a problem with PHP. This sort of thing should be built in with the language. Better yet, an IDE (which handles ideas the designers never imagined) could handle it. All in a way which works with a CPU 10 years old...<sigh>. But back to your problem. What is your top level problem? What are you trying to solve? Let's talk about that, then we can talk about actual code. (Which you'll probably have to cough up as well...if you're reluctant to cough up code, think about it this way...if we couldn't [together] write better code, then why are you asking advice? Hmm?)