General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old February 21st, 2003, 05:59 PM
baddogg99 baddogg99 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: GA, USA
Posts: 19 baddogg99 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
2 queries, loop once for every record

I have 2 queries. One is a list of books, the other is a list of stores.

I want to display the list of books in table rows (no problem), and list each store in columns next to the book name.

example:

book1 store1 store2 store3
book2 store1 store2 store3
book3 store1 store2 store3
book4 store1 store2 store3

I am having a problem looping through the mysql array for the stores on every row....it just comes back blank. when I pull the loop outside of the book loop, it works, so I guess I don't understand how to nest them correctly.

Can someone show me a really rough example so I can see what I am doing wrong?

Thanks in advance,
B

Reply With Quote
  #2  
Old February 22nd, 2003, 02:56 AM
ramz ramz is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Location: Juiz de Fora - MG- Brazil
Posts: 93 ramz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 16
Send a message via ICQ to ramz Send a message via MSN to ramz
Try this:

$sql1 = "SELECT * FROM table1";
$query1 = mysql_query($sql1,$connection);
if(mysql_num_rows($query1)) {
print "<table>";
while($rs1 = mysql_fetch_array($query1)) {
print "<tr><td>";
print $rs1['book'];
print "</td>";
$sql2 = "SELECT * FROM table2";
$query2 = mysql_query($sql2,$connection);
if(mysql_num_rows($query2)) {
while($rs2 = mysql_fetch_array($query2)) {
print "<td>";
print $rs2['store'];
print "</td>";
}
}
print "</tr>";
}
} else {
print "No results!";
}
__________________
Regards,
Ramiro Varandas Jr.

Last edited by ramz : February 22nd, 2003 at 02:58 AM.

Reply With Quote
  #3  
Old February 23rd, 2003, 08:51 AM
baddogg99 baddogg99 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: GA, USA
Posts: 19 baddogg99 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question

Is there a way to just query the stores once, then refer to the query several times? It seems to me to be inefficient to query the database every time I loop through the book names...

Reply With Quote
  #4  
Old February 23rd, 2003, 12:01 PM
ramz ramz is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Location: Juiz de Fora - MG- Brazil
Posts: 93 ramz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 16
Send a message via ICQ to ramz Send a message via MSN to ramz
Yes! sure!

PHP Code:
 $sql1 "SELECT * FROM table1";
$sql2 "SELECT * FROM table2";
$query1 mysql_query($sql1,$connection);
$query2 mysql_query($sql2,$connection);
if(
mysql_num_rows($query2)) {
    
$i 0;
    while(
$rs2 mysql_fetch_array($query2)) {
        
$stores[$i] = $rs2['store'];
        
$i++;
    }
} else {
    
$stores[0] = "&nbsp;";
}
if(
mysql_num_rows($query1)) {
    print 
"<table>";
    while(
$rs1 mysql_fetch_array($query1)) {
        print 
"<tr><td>";
        print 
$rs1['book'];
        print 
"</td>";
        for(
$i 0$i sizeof($stores); $i++) {
            print 
"<td>";
            print 
$stores[$i];
            print 
"</td>";
        }
    }
    print 
"</tr>";
} else {
    print 
"No results!";


Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > 2 queries, loop once for every record


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap