• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Mysql query result in html table in MY way

Hello everyone,
I did a search in the forum but couldn't find what I'm looking for, nor in any other forum through a google browsing.

What I'd like to do is:
1) query a database mysql
2) Display the results in this way:

image1 | image2 | image3 (3 columns only)
label1 | label2 | label3
info1 | info2 | info3
price1 | price2 | price3 (4 rows for each item)
image4 | image5 | image6
label4................... ( and so on....)

image,label,info, price are columns from the SAME table retrieved through the query (values are inserted in a row)

I know how to populate the table the usual way, row after row, but I'm lost with my ideal formatting...

A thousands thanks to who can give me the code I need, I'm desperate Sad

Something like this:

$count = 0;
foreach($query->result() as row)
  $td1 .= '<td>'.$row->img1.'</td>';
  $td2 .= '<td>'.$row->label2.'</td>';
  $td3 .= '<td>'.$row->info.'</td>';
  echo '<tr>',$td1,'</tr>';
  echo '<tr>',$td2,'</tr>';
  echo '<tr>',$td3,'</tr>';
  $count = 0;
//print last records
  echo '<tr>',$td1,'</tr>';
  echo '<tr>',$td2,'</tr>';
  echo '<tr>',$td3,'</tr>';

Just add the price and some checks.

Thnak you for the answer! I'm at work now, so I cannot check the code.
Being quite a n00b, I'm missing something concerning what comes before your script.

Let's give a sample:

require ('logindata.php'); //page with datas for connection
require ('dbconnect.php'); //page with database connect. instructions

$query="SELECT img,name,info,price FROM table WHERE color=blue";
// I start the query

if(!&resultsWink {
die ("Query problems"); }

// so far, so good.
// usually, I did a $row=mysqli_fetch_assoc($query);
// then declared single variables for each column, i.e.
// $image=$row['img'];
// and so on, echoing them in <td>
// Truly, I dont' know where I can insert your snippet, though I think to start grasping its meaning

Thanks again!

Dude, I gave you a CI example. Not plain PHP.

I suggest you start with CI. Then we can talk.

Ok, I'll try codeIgniter and then come back, in case! thanks again

Hi damien

whether you are using CI or your own implementation at the end of the day you will have your results in an associative array so you got that that stage.

First of all do you HAVE to use a table? If not you can use div.

<div class="oneResult"> Info for one record. label info. etc etc </div>

Then to format them in rows of three you can use CSS to achieve the positioning (eg. using float and clear attributes). Or you can use <span> and insert a break <br/>

If you need, inside your loop you can determine if its the third (or first depending on which u prefer) then for this element just modify the class or style.

if ( $counter % 3 == 0 ): // then insert style="..." or class=" .. "  inside the tag

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.