howto get my loop in a loop working?

11-25-11, 06:48 AM
|
|
Newbie Coder
|
|
Join Date: Nov 2011
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
howto get my loop in a loop working?
Hi all,
I have several divs (<div id="tab1" class="tab_content">). Each div has a loop (hope this is the correct term) in it, getting data from my database:
Now each div has it's own id: tab1, tab2 etc. I want to make it so that these divs are also from the database, loop them and show them in the php file so I can remove a lot of HtmL code.
I can get the tab id's like this:
The problem is that I can't seem to get this working with a while loop since it already has a while in the div itself. I know it's possible, I have seen people do this (though with a foreach), so how can I do this properly? If a foreach is required that is ok.
Also note this part in the div loop:
The tabID should change to 2 if it's in the div with id tab2 etc
Hope someone can help me out
Kind regards,
Maurice
|

11-25-11, 07:55 AM
|
|
Newbie Coder
|
|
Join Date: Nov 2011
Posts: 98
Thanks: 0
Thanked 9 Times in 9 Posts
|
|
a few problems here:
1.you are making a separate query each time. This wont be a deal if its a small database with 200 records in it. a large one will take so long that you are gonna want to shoot yourself while waiting. i suggest optimizing your queries in one(investigate mysql join's).
2.in your particular case-there is no problem with using while inside a while... the problem in your case is that in both while loops you've named the variable $row. so the server gets confuised and cant carry on. you'll have to give the variables different names and it should work fine...
|

11-25-11, 08:54 AM
|
|
Newbie Coder
|
|
Join Date: Nov 2011
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Thank you for your reply,
My problem is a bit that my knowledge about mysql and queries is a bit... noobish. The 2 tables can be joined by the TabID, the questions I have then is, how to do this and how to publish this with a foreach/while in my php script?
My database tables look like this:
and
|

11-25-11, 02:51 PM
|
|
Newbie Coder
|
|
Join Date: Nov 2011
Posts: 98
Thanks: 0
Thanked 9 Times in 9 Posts
|
|
well they are simple once you get into that mindset... in your case it would be something like
now instead of t1.* you could do t1.id, t1.whatever, t1.whatever2 and so on. same with t2. t1 and t2 are just how ive called them but you can name them however you like. Now what you need to be careful with are the WHERE clauses. let's assume you choose to use t1 and t2. when you get to the where clause it should look like t1.id='2' for example. each time you have to state the table and the field you are interested in.
|

11-25-11, 06:16 PM
|
|
Newbie Coder
|
|
Join Date: Nov 2011
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
You have lost me here, what do you mean with t1 and t2? The divs? Their id's are 1, 2, 3 etc (there are more then 2 divs) in the db
|

11-26-11, 04:05 AM
|
|
Newbie Coder
|
|
Join Date: Nov 2011
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Okay, played around a bit and made a join query that looks like this:
My whole script now looks like this:
Only I get an error:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in blabla/tabtest.php on line 123
The line 123 is where the while part begins...
I am thinking my join is not correct, since I get no result when echo the $result2
|

11-26-11, 05:02 AM
|
|
Newbie Coder
|
|
Join Date: Nov 2011
Posts: 98
Thanks: 0
Thanked 9 Times in 9 Posts
|
|
the reason why you are getting an error is because your query is wrong... you need to give reference to each table and each column in the table... in other words:
take a look:
http://img207.imageshack.us/img207/844/screenshot9e.png
it could be t1 or table1 or whatever1-if you dont the mysql server cant figure out what you are trying to do... if you run your query as it is you get:
another suggestion-store the sql instructions in separate values and if something is wrong-echo them out and run them from the mysql and see whats wrong:
|
|
The Following User Says Thank You to alxkls For This Useful Post:
|
|

11-26-11, 10:07 AM
|
|
Newbie Coder
|
|
Join Date: Nov 2011
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
My reply seems to be lost while posting so here a new one
The query worked, checked it in PHP my admin and iy gave the whole table. So thanks for sticking with me and explaining it helped a lot.
Now it seems my loop is wrong. Since my original loop in a loop was a bit database inefficient, I removed it and put all in 1 loop. This of course is wrong cause it loops the whole table now row by row pushing it's content.
So how to fix this?
Currently it shows every row from the table in the loop (see the html from: http://www.mauricederegt.nl/dev/mahjong/tabtest.php), but it should loop the id first and all loop its content that is attached to that id while its looping the id's (do I make any sense??) Hence the loop in the loop.
My latest code:
Many thanks
Last edited by mauricederegt; 11-26-11 at 10:16 AM.
|

11-26-11, 12:03 PM
|
|
Newbie Coder
|
|
Join Date: Nov 2011
Posts: 98
Thanks: 0
Thanked 9 Times in 9 Posts
|
|
sense-it makes none to me... idk kind of hard to follow someone elses logic, especially when ive not no idea what the final result should be... im mostly guessing but... you may need to figure out which table you need to select from initially and which to join next... more of a relationship logic needed-one on one, one on many, many on one.... try to organize that in your head logically and then apply it...
|

11-26-11, 01:03 PM
|
|
Newbie Coder
|
|
Join Date: Nov 2011
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Yeah I figured that, I'll make a better step by step description to make things clear and come back to this tomorrow.
|
|
Currently Active Users Viewing This Thread: 2 (0 members and 2 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|