Current location: Hot Scripts Forums » Programming Languages » PHP » howto get my loop in a loop working?


howto get my loop in a loop working?

Reply
  #1 (permalink)  
Old 11-25-11, 06:48 AM
mauricederegt mauricederegt is offline
Newbie Coder
 
Join Date: Nov 2011
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
Question 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:

PHP Code:

<div id="tab1" class="tab_content"> 

            <ul class="columns"> 
            <?php 
                $result 
mysql_query("SELECT * FROM TabContent WHERE TabID = 1");
                while(
$row mysql_fetch_array($result))
                  {
                  echo 
'<li><a href="#"><img src="images/layouts/'$row['LayoutName'] .'.png" alt="" /></a>
                            <div class="info"> 
                                <h2>'
$row['LayoutName'] .'</h2> 
                                <p>'
$row['LayoutTiles'] . 'Tiles, '$row['LayoutLayers'] . 'Layers</p>
                            </div> 
                        </li>'
;
                  }
            
?>  
            </ul>
            <div class="clear"></div>
 </div> 

<div id="tab2" class="tab_content"> 
            <ul class="columns"> 
            <?php 
                $result 
mysql_query("SELECT * FROM TabContent WHERE TabID = 2");
                while(
$row mysql_fetch_array($result))
                  {
                  echo 
'<li><a href="#"><img src="images/layouts/'$row['LayoutName'] .'.png" alt="" /></a>
                            <div class="info"> 
                                <h2>'
$row['LayoutName'] .'</h2> 
                                <p>'
$row['LayoutTiles'] . 'Tiles, '$row['LayoutLayers'] . 'Layers</p>
                            </div> 
                        </li>'
;
                  }
            
?>  
            </ul>
            <div class="clear"></div>
 </div> 

<div id="tab3" class="tab_content"> 
 etc etc etc
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:

PHP Code:

<?php 

                $result 
mysql_query("SELECT * FROM Tabs");
                while(
$row mysql_fetch_array($result))
                  {
                  echo 
'tab'$row['TabID'] .;
                  }
            
?>
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:

PHP Code:

 $result mysql_query("SELECT * FROM TabContent WHERE TabID = 1"); 

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
Reply With Quote
  #2 (permalink)  
Old 11-25-11, 07:55 AM
alxkls alxkls is offline
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...
Reply With Quote
  #3 (permalink)  
Old 11-25-11, 08:54 AM
mauricederegt mauricederegt is offline
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:
PHP Code:

CREATE TABLE TabContent 

(
TabContentID int NOT NULL AUTO_INCREMENT
PRIMARY KEY(TabContentID),
LayoutName varchar(15),
LayoutTiles int,
LayoutLayers int,
TabID int

and
PHP Code:

CREATE TABLE Tabs 

(
TabID int NOT NULL AUTO_INCREMENT
PRIMARY KEY(TabID),
TabName varchar(15)

Reply With Quote
  #4 (permalink)  
Old 11-25-11, 02:51 PM
alxkls alxkls is offline
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

Code:
SELECT `t1`.*,` t2`.* FROM `TabContent` `t1` LEFT JOIN `Tabs` `t2` ON `t1`.`TabID`=`t2`.`TabID`
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.
Reply With Quote
  #5 (permalink)  
Old 11-25-11, 06:16 PM
mauricederegt mauricederegt is offline
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
Reply With Quote
  #6 (permalink)  
Old 11-26-11, 04:05 AM
mauricederegt mauricederegt is offline
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:
PHP Code:

$result2 mysql_query("SELECT * FROM Tabs INNER JOIN TabContent ON Tab.TabID = TabContent.TabID"); 

My whole script now looks like this:
PHP Code:

<?php 

            $result2 
mysql_query("SELECT * FROM Tabs INNER JOIN TabContent ON Tab.TabID = TabContent.TabID");
            while(
$row2 mysql_fetch_array($result2))
              {
              echo 
'<div id="tab'$row2['TabID'] .'" class="tab_content"> 
                  <ul class="columns">
                  <li><a href="#"><img src="images/layouts/'
$row2['LayoutName'] .'.png" alt="" /></a>
                                    <div class="info"> 
                                        <h2>'
$row2['LayoutName'] .'</h2> 
                                        <p>'
$row2['LayoutTiles'] . 'Tiles, '$row2['LayoutLayers'] . 'Layers</p>
                                    </div> 
                                </li>
                           </ul>
                           <div class="clear"></div>
                           <div class="bottom">
                               <div class="clearfix">
                                    <div class="lfloat"></div>
                                    <div class="rfloat"><a class="hide" href="#" onclick="return false">Cancel</a></div>
                                </div>
                          </div>    
                      </div> '
;
                        }
        
?>
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
Reply With Quote
  #7 (permalink)  
Old 11-26-11, 05:02 AM
alxkls alxkls is offline
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:
Code:
SELECT t1.*, t2.* FROM Tabs t1 LEFT JOIN TabContent t2 ON t1.TabID = t2.TabID
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:

Code:
mysql> SELECT * FROM Tabs INNER JOIN TabContent ON Tab.TabID = TabContent.TabID
    -> ;
ERROR 1054 (42S22): Unknown column 'Tab.TabID' in 'on clause'
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:

Code:
 $sql="SELECT t1.*, t2.* FROM Tabs t1 LEFT JOIN TabContent t2 ON t1.TabID = t2.TabID";
 echo $sql;
 $result2 = mysql_query($sql);
Reply With Quote
The Following User Says Thank You to alxkls For This Useful Post:
mauricederegt (11-26-11)
  #8 (permalink)  
Old 11-26-11, 10:07 AM
mauricederegt mauricederegt is offline
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:
PHP Code:

<div id="divNav" class="tab_container"> 
        <?php 
            $result2 
mysql_query("SELECT t1.*, t2.* FROM Tabs t1 LEFT JOIN TabContent t2 ON t1.TabID = t2.TabID");
            while(
$row2 mysql_fetch_array($result2))
              {
              echo 
'<div id="tab'$row2['TabID'] .'" class="tab_content"> 
                          <ul class="columns">
                              <li><a href="#"><img src="images/layouts/'
$row2['LayoutName'] .'.png" alt="" /></a>
                                    <div class="info"> 
                                        <h2>'
$row2['LayoutName'] .'</h2> 
                                        <p>'
$row2['LayoutTiles'] . 'Tiles, '$row2['LayoutLayers'] . 'Layers</p>
                                    </div> 
                                </li>
                           </ul>
                           <div class="clear"></div>
                           <div class="bottom">
                               <div class="clearfix">
                                    <div class="lfloat"></div>
                                    <div class="rfloat"><a class="hide" href="#" onclick="return false">Cancel</a></div>
                                </div>
                          </div>    
                      </div> '
;
                        }
        
?>    
        </div>
Many thanks

Last edited by mauricederegt; 11-26-11 at 10:16 AM.
Reply With Quote
  #9 (permalink)  
Old 11-26-11, 12:03 PM
alxkls alxkls is offline
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...
Reply With Quote
  #10 (permalink)  
Old 11-26-11, 01:03 PM
mauricederegt mauricederegt is offline
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.
Reply With Quote
Reply

Bookmarks

Tags
foreach, loop, mysql, php


Currently Active Users Viewing This Thread: 2 (0 members and 2 guests)
 
Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
assembly language using notepad++, TASM, and TLINK caierhui Other Languages 0 03-18-10 10:50 AM
Iterating Associative arrays with foreach divya PHP 13 11-21-07 01:12 PM
infinite loop versus guarded blocks UnrealEd Everything Java 0 11-11-07 02:53 PM
Update multiple rows outside loop - need help ElvansX PHP 1 12-03-06 01:55 AM
for loop not working in php/MySQL Newbie2005 PHP 0 11-10-05 03:28 AM


All times are GMT -5. The time now is 08:57 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.