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

#1
[eluser]Damien_Pirsy[/eluser]
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

#2
[eluser]rogierb[/eluser]
Something like this:

Code:
$count = 0;
$td1=$td2=$td3=null;
foreach($query->result() as row)
{
if($count<3)
{
  $td1 .= '<td>'.$row->img1.'</td>';
  $td2 .= '<td>'.$row->label2.'</td>';
  $td3 .= '<td>'.$row->info.'</td>';
  ++count;
}
else
{
  echo '<tr>',$td1,'</tr>';
  echo '<tr>',$td2,'</tr>';
  echo '<tr>',$td3,'</tr>';
  $td1=$td2=$td3=null;
  $count = 0;
}
}
//print last records
  echo '<tr>',$td1,'</tr>';
  echo '<tr>',$td2,'</tr>';
  echo '<tr>',$td3,'</tr>';

Just add the price and some checks.

#3
[eluser]Damien_Pirsy[/eluser]
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:

&lt;?php
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

$results=mysqli_query($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
//
?&gt;

Thanks again!

#4
[eluser]rogierb[/eluser]
Dude, I gave you a CI example. Not plain PHP.

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

#5
[eluser]Damien_Pirsy[/eluser]
Ok, I'll try codeIgniter and then come back, in case! thanks again

#6
[eluser]andrewtheandroid[/eluser]
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.

Code:
<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.

Code:
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.