Solved

Putting results into an HTML table

Posted on 2009-05-05
5
302 Views
Last Modified: 2012-05-06
I am retrieving data from my db using two queries:

1. SELECT * FROM DEALERS
2. SELECT * FROM CAMPAIGNS WHERE dealer_id = x ( pulled from 1st query )

What I want to do is put the results in the table below, but am unsure how to do this.

Can anyone show me how to do this in PHP?

Thank you.
<table class="listing" cellpadding="0" cellspacing="0">
    <tr>
      <th class="first" width="177">Dealer Campaigns</th>
 
      <th>Campaign Name</th>
 
      <th>Date Sent</th>
 
      <th># Sent</th>
 
      <th>View</th>
 
      <th class="last">Export</th>
    </tr>
 
    <tr>
      <td class="first style1">- Sperry Ford- ID#12233456</td>
 
      <td>&nbsp;</td>
 
      <td>&nbsp;</td>
 
      <td>&nbsp;</td>
 
      <td>&nbsp;</td>
 
      <td class="last">&nbsp;</td>
    </tr>
 
    <tr class="bg">
      <td class="first style2">- Id: 1234567</td>
 
      <td>Campaign Name Here</td>
 
      <td>5/01/2009</td>
 
      <td>322</td>
 
      <td><a href="#"><img src="img/save-icon.gif" width="16" height="16" alt=
      "save" /></a></td>
 
      <td class="last"><a href="#"><img src="img/add-icon.gif" width="16" height="16"
      alt="add" /></a></td>
    </tr>
 
    <tr class="bg">
      <td class="first style2">- Id: 5687552254</td>
 
      <td>Campaign Name Here</td>
 
      <td>5/01/2009</td>
 
      <td>211</td>
 
      <td><a href="#"><img src="img/save-icon.gif" width="16" height="16" alt=
      "save" /></a></td>
 
      <td class="last"><a href="#"><img src="img/add-icon.gif" width="16" height="16"
      alt="add" /></a></td>
    </tr>
 
    <tr>
      <td class="first style1">- Another Dealer - ID#44556</td>
 
      <td>&nbsp;</td>
 
      <td>&nbsp;</td>
 
      <td>&nbsp;</td>
 
      <td>&nbsp;</td>
 
      <td class="last">&nbsp;</td>
    </tr>
 
    <tr class="bg">
      <td class="first style2">- Id: 9876540</td>
 
      <td>Campaign Name Here</td>
 
      <td>6/01/2008</td>
 
      <td>223</td>
 
      <td><a href="#"><img src="img/save-icon.gif" width="16" height="16" alt=
      "save" /></a></td>
 
      <td class="last"><a href="#"><img src="img/add-icon.gif" width="16" height="16"
      alt="add" /></a></td>
    </tr>
  </table>

Open in new window

table.jpg
0
Comment
Question by:edvinson
[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
  • 3
  • 2
5 Comments
 
LVL 6

Expert Comment

by:mosidiot
ID: 24303843
ok.. just give you are rough idea..

$query=mysql_query("SELECT * FROM CAMPAIGNS WHERE dealer_id = x",$connectionstring)

echo "<table>";

while($row=mysql_fetch_array($query){

echo "<tr><td>$row[id]</td><td>$row[campaigname]</td></tr>"; // where the id and the campaignname is your sql fields...

}

echo "<table";

You need  to modify to make it right..
0
 
LVL 1

Author Comment

by:edvinson
ID: 24304764
I understand, but there are possibly more than one dealer, and X amount of campaigns per dealer.

I am guessing somewhere in the echo-ing needs to be some conditional logic???
0
 
LVL 6

Expert Comment

by:mosidiot
ID: 24304816
$query=mysql_query("SELECT * FROM CAMPAIGNS WHERE dealer_id = x",$connectionstring)

echo "<table>";

$dealer="";

while($row=mysql_fetch_array($query){

if($dealer!==$r["dealer"]){

echo "something else for dealer here";

$dealer = $row["dealer"];

}else{

echo "<tr><td>$row[id]</td><td>$row[campaigname]</td></tr>"; // where the id and the campaignname is your sql fields...

}

echo "<table>";

Do you mean this?
0
 
LVL 1

Author Comment

by:edvinson
ID: 24304879
well sort of. If you look at the photo, you will see that in the table there are two dealers. Each has campaigns below.

So I would have two loops I think,

dealer loop

    do tr for dealer
      campaign loop
      do tr for campaigns
    end campaign loop

end loop

Do you see what I mean? The dealer row will be different (as in the code example, different class etc).

Hope that helps
0
 
LVL 6

Accepted Solution

by:
mosidiot earned 500 total points
ID: 24305551
ya.. that's what my example will do...

just simply change your sql statement to SELECT * FROM CAMPAIGNS WHERE dealer_id = x order by dealerid

in that case will produce the ideal result.. otherwise it will be randomly sorted like the following...

dealer1
info1
info2
dealer2
info1
dealer1
info3
dealer2
info2
dealer3
info1

if you sort them accordingly... it will be:

dealer1
info1
info2
info3
dealer2
info1
info2
dealer3
info1

0

Featured Post

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

691 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