1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Hey Guest Last month we upgraded BlackHatWorld.com to a new platform - . If you notice anything that requires attention please start a new thread here.
    Dismiss Notice

While loops objects next to each other

Discussion in 'PHP & Perl' started by Fadi946, Jan 27, 2013.

  1. Fadi946

    Fadi946 Power Member

    Joined:
    Mar 25, 2009
    Messages:
    526
    Likes Received:
    77
    Location:
    With my parents :(
    Hey guys so I'm trying to make a list of books in my website (getting them from my database), and I succeded to put them but they are displaying one under the other... and I want to put them side by side.

    Just in case you didn't understand me, I will give you an example:

    What I have now is:
    Book1
    Book2
    book3

    What I want to do is:
    Book1 -> Book2 -> Book3

    This is my code:

    PHP:

                   include 'DBConnection.php';            
    $ShowTopic = 'SELECT * FROM Books WHERE age = "Children"';      
          $Topicquery = mysql_query($ShowTopic);      
          while($DisTopic=mysql_fetch_array($Topicquery)){     
           ?>       
        <div class="new_products">         
               <div class="new_prod_box">         
                   <a href="details.php?book=<?php echo $DisTopic['getTitle']?>"><?php echo $DisTopic['Title'];?></a>   
                         <div class="new_prod_bg1">         
                    <a href="details.php?book=<?php echo $DisTopic['getTitle']?>"><img src="<?php echo $DisTopic['Image'];?>" alt="" width="98" height="150" title="" class="thumb" border="0" /></a>   
                         </div>                   
                </div>                  
                   </div>      
           <?php       
         
    }     
       
    ?>  
    I've tested many way to do it, and I noticed that the problem is the "while".. because without it, it will be good... but I really need this while loop.

    Anyone can help me please? Its for my final project and I will gladly appreciate your help :)
    Thanks!

    -Fadi
     
  2. Kilise

    Kilise Newbie

    Joined:
    Dec 10, 2011
    Messages:
    34
    Likes Received:
    5
    Try this

    <?php
    include 'DBConnection.php';
    $Topicquery = mysql_query('SELECT * FROM Books WHERE age = "Children"');
    if(mysql_num_rows($Topicquery) > 0){
    while($DisTopic=mysql_fetch_assoc($Topicquery)){
    ?>

    <div class="new_products">
    <div class="new_prod_box">
    <a href="details.php?book=<?php echo $DisTopic['getTitle']; ?>"><?php echo $DisTopic['Title']; ?></a>
    <div class="new_prod_bg1">
    <a href="details.php?book=<?php echo $DisTopic['getTitle']; ?>"><img src="<?php echo $DisTopic['Image']; ?>" alt="" width="98" height="150" title="" class="thumb" border="0" /></a>
    </div>
    </div>
    </div>
    <?php
    }
    }
    ?>
     
    • Thanks Thanks x 1
  3. Fadi946

    Fadi946 Power Member

    Joined:
    Mar 25, 2009
    Messages:
    526
    Likes Received:
    77
    Location:
    With my parents :(
    Thanks for answering, but it did not really work :( Still the same problem.
     
  4. Kilise

    Kilise Newbie

    Joined:
    Dec 10, 2011
    Messages:
    34
    Likes Received:
    5
    What error message do you get, what kiind of problem is it?

    EDIT: sorry didnt read good, i'll tryu to fix it now.
     
    Last edited: Jan 27, 2013
  5. Gogol

    Gogol Elite Member

    Joined:
    Sep 10, 2010
    Messages:
    3,002
    Likes Received:
    2,852
    Would you please make the code a bit more readable? Can't see it actually n havin to copy it to my editor:p
     
  6. Kilise

    Kilise Newbie

    Joined:
    Dec 10, 2011
    Messages:
    34
    Likes Received:
    5
    You have to work with the css. The style. Try to float the divs left
     
    • Thanks Thanks x 1
  7. Gogol

    Gogol Elite Member

    Joined:
    Sep 10, 2010
    Messages:
    3,002
    Likes Received:
    2,852
    not tested. Let me know if this works
    Code:
    <?
    
    
                   include 'DBConnection.php';            
    $ShowTopic = 'SELECT * FROM Books WHERE age = "Children"';      
          $Topicquery = mysql_query($ShowTopic);      
          ?>
    	  <div class="new_products">  
    	  <?
    	  while($DisTopic=mysql_fetch_array($Topicquery)){     
           ?>       
               
               <div class="new_prod_box">         
                   <a href="details.php?book=<?php echo $DisTopic['getTitle']?>"><?php echo $DisTopic['Title'];?></a>   
                         <div class="new_prod_bg1">         
                    <a href="details.php?book=<?php echo $DisTopic['getTitle']?>"><img src="<?php echo $DisTopic['Image'];?>" alt="" width="98" height="150" title="" class="thumb" border="0" /></a>   
                         </div>                   
                </div>                  
                       
           <?php       
         }     
       ?>
       <div style="clear:both"></div>
       </div>  
       
       <style> .new_prod_box{float:left;width:250px; height:100px; display:block;}</style>
    
     
    • Thanks Thanks x 1
  8. 0_00_0

    0_00_0 Senior Member

    Joined:
    Oct 7, 2010
    Messages:
    1,023
    Likes Received:
    486
    Location:
    Canada
    use css :)

    <div class="bookthumbnail">...bookstuff</div>

    .bookthumbnail{
    width: 33%;
    float: left;
    }
     
    • Thanks Thanks x 1
    Last edited: Jan 27, 2013
  9. Fadi946

    Fadi946 Power Member

    Joined:
    Mar 25, 2009
    Messages:
    526
    Likes Received:
    77
    Location:
    With my parents :(
    Thanks so much g0g0l!!! I've been trying to fix it for so long, and now it works!! You're awesome! +rep!
    (Thanks to everyone who tried to help as well! Much appreciated!)
     
  10. Gogol

    Gogol Elite Member

    Joined:
    Sep 10, 2010
    Messages:
    3,002
    Likes Received:
    2,852
    Thanks budd :)
     
  11. gabmasm

    gabmasm Registered Member

    Joined:
    Feb 19, 2012
    Messages:
    60
    Likes Received:
    29
    float left or using a table tag?
     
  12. Fadi946

    Fadi946 Power Member

    Joined:
    Mar 25, 2009
    Messages:
    526
    Likes Received:
    77
    Location:
    With my parents :(
    putting the "while" AFTER the "new_product" div solved it.