Page 1 of 2

Split database content.

Posted: Tue Dec 23, 2008 3:28 am
by newphpnoob
Hello,

I have been trying to find a way to split my content from a database into two divs but for some reason I am getting multple divs and its causes a layout issue.

This is what I have now;

Code: Select all

 
            <?php
 
                echo "<div id=\"ccol-two\">"; 
                
                $sql = mysql_query("select * from categories");
                while ($row = mysql_fetch_array($sql))
                {
                    $i=1; 
                    $id = $row[0];
                    $name = $row[1];
                
                    echo "<br /><span class=\"bold\">$name</span><br/>";
                    echo "<span class=\"bold\">*Course Code / Course Name</span> <br />";
                
                    $innersql = mysql_query("select * from classes where categoryid = '$id'");              
                    $num_rows = mysql_num_rows($innersql); 
                    $percol = $num_rows/2; 
            
                    
                    while ($row2 = mysql_fetch_assoc($innersql)) 
                    {                       
                        $name = $row2['coursename'];
                        $link = $row2['link'];
            
                        echo "<a href = \"$link\" target=\"_blank\">$name</a> <br />";      
                
                    }
 
                        if ($i > $percol) 
                        { 
                            echo "</div><div id=\"ccol-one\">"; 
                        }
                            
                    $i++;
                 }
                     
                 echo "</div>";
                 
                 
            ?>
 
 

Any help would be really appreciated. Thanks. :)

Re: Split database content.

Posted: Wed Dec 24, 2008 8:09 pm
by novice4eva

Code: Select all

 
$echoed = false;
$sql = mysql_query("select * from categories");
                while ($row = mysql_fetch_array($sql))
                {
....
....
if ($i > $percol)
                        {
                            echo (!$echoed)?"</div><div id=\"ccol-one\">":NULL;
                            $echoed = true;
                        }
...
...
 

Re: Split database content.

Posted: Wed Dec 24, 2008 10:27 pm
by newphpnoob
Sorry but I am kind of lost on this. I put what you had and nothing is outputted from the database.

Re: Split database content.

Posted: Thu Dec 25, 2008 3:15 am
by novice4eva
did u put .... too?? :D
EDIT:

Code: Select all

 
                $echoed = false;
                echo "<div id=\"ccol-two\">";
               
                $sql = mysql_query("select * from categories");
                while ($row = mysql_fetch_array($sql))
                {
                    $i=1;
                    $id = $row[0];
                    $name = $row[1];
               
                    echo "<br /><span class=\"bold\">$name</span><br/>";
                    echo "<span class=\"bold\">*Course Code / Course Name</span> <br />";
               
                    $innersql = mysql_query("select * from classes where categoryid = '$id'");             
                    $num_rows = mysql_num_rows($innersql);
                    $percol = $num_rows/2;
           
                   
                    while ($row2 = mysql_fetch_assoc($innersql))
                    {                      
                        $name = $row2['coursename'];
                        $link = $row2['link'];
           
                        echo "<a href = \"$link\" target=\"_blank\">$name</a> <br />";      
               
                    }
 
                        if ($i > $percol)
                        {
                             echo (!$echoed)?'</div><div id="ccol-one">':'';
                             $echoed = true;
                        }
                           
                    $i++;
                 }
                     
                 echo "</div>";
 
Not much of a change from the original post, should work !!

Re: Split database content.

Posted: Thu Dec 25, 2008 2:43 pm
by newphpnoob
Oh you are boss. LOL That was awesome.

But I noticed that It only put three things on one column and the rest on the other. Is there a way where I distribute them to have even amount of content on both?

Re: Split database content.

Posted: Thu Dec 25, 2008 8:36 pm
by novice4eva
with divs and spans its really hard to get a perfect row/column view like table, and with so many <br> and <spans> i can only see things coming out in rows rather than columns(didn't try that code in my comp just calculated guess)...ehh use table instead, and pray this time that the designers wouldn't give you evil eyes, unless you are the designer, if that's the case - forgive yourself once :D

Re: Split database content.

Posted: Thu Dec 25, 2008 8:52 pm
by Syntac
Or try:

Code: Select all

<div style="float: left;"><?php echo $data_in_first_column; ?></div>
<div style="float: left;"><?php echo $data_in_second_column; ?></div>

Re: Split database content.

Posted: Fri Dec 26, 2008 2:21 am
by newphpnoob
novice4eva - I am just curious why then it only have three on one column and rest goes to the second column?

Syntac - When you say try the method you have. What would i put the variable to equal to?

Re: Split database content.

Posted: Fri Dec 26, 2008 8:02 am
by novice4eva
must have to do with css, check out the property of #ccol-one and #ccol-two

Re: Split database content.

Posted: Fri Dec 26, 2008 3:40 pm
by newphpnoob
The css has this;

Code: Select all

 
#ccol-one {
float: right;
width:445px;
height: auto;
margin: 10px 0px 0px 0px;
background-color: aqua;
}
 
#ccol-two {
float: right;
width: 445px;
height: auto;
margin: 10px 0px 0px 0px;
background-color: gray;
}
 
 
This is the image what it is doing. http://atlasflooring.com/col2.png you see my point?

Re: Split database content.

Posted: Fri Dec 26, 2008 7:26 pm
by Andrewrun
Why are you setting $i = 1 inside the while loop? Try setting that outside the outer while loop (on line 2 or something). Otherwise i will always get reset to 1 when the loop starts.

Re: Split database content.

Posted: Sat Dec 27, 2008 2:49 am
by newphpnoob
Andrewrun -I tried that I get only two section on the second column and the rest in the first column.

Re: Split database content.

Posted: Tue Dec 30, 2008 1:36 am
by Andrewrun

Code: Select all

 
                $echoed = false;
                echo "<div id=\"ccol-two\">";
 
                $i=1;
 
                $sql = mysql_query("select * from categories");
 
                $num_rows = mysql_num_rows($sql);
                $percol = $num_rows/2;
 
                while ($row = mysql_fetch_array($sql))
                {
                    $id = $row[0];
                    $name = $row[1];
               
                    echo "<br /><span class=\"bold\">$name</span><br/>";
                    echo "<span class=\"bold\">*Course Code / Course Name</span> <br />";
               
                    $innersql = mysql_query("select * from classes where categoryid = '$id'");             
                   
                    while ($row2 = mysql_fetch_assoc($innersql))
                    {                      
                        $name = $row2['coursename'];
                        $link = $row2['link'];
           
                        echo "<a href = \"$link\" target=\"_blank\">$name</a> <br />";      
               
                    }
 
                    if ($i > $percol)
                    {
                         echo (!$echoed) ? '</div><div id="ccol-one">' : '';
                         $echoed = true;
                    }
                           
                    $i++;
                 }
                     
                 echo "</div>";
 
Try this. I just changed a few things around. I think what you're trying to do here is select the overall list of classes from the categories table and then output details about each class from the classes table.

I think you'll have better luck getting the number of rows in the first query. Then divide that in half and set that as the percol value. But set that based on the first sql query, not the second one.

Another thing, you should look into doing a JOIN command in your first sql query to save time when the page is loading. Check out sqlzoo.net. But basically you could do something like this:

Code: Select all

 
$sql = "SELECT cat.*, class.* 
            FROM categories cat, class 
            WHERE cat.id = class.categoryid";
$result = mysql_query($sql);
 
$num_rows = mysql_num_rows($result);
...
 
while($row = mysql_fetch_array($result) {
    $name = $row[1];
    $link = $row['link'];
    $name2 = $row['name'];
    ....
}
 
That's just a really rough example. I don't know how your table is setup so I can't be any more specific than that, but just food for thought.

Re: Split database content.

Posted: Tue Dec 30, 2008 4:14 pm
by newphpnoob
AWESOME! Thanks that solved it.

I just have some questions about the code it self;

What is the question mark mean? I did for a resaons just toatlly forgot it :oops:

Code: Select all

 
echo (!$echoed) ? '</div><div id="ccol-one">' : '';
 
I changed it to this;

Code: Select all

 
                    if ($i > $percol)
                    {
                         
                         if (!$echoed) 
                         {
                            echo    "</div><div id=\"ccol-one\">";
                         }
                         
                         $echoed = true;
                    }
 

Also is there a way where I can flip the order of the way how the data is being outputted? Becuase I want the stuff in the right column to be on the left column and vice a versa.

Re: Split database content.

Posted: Tue Dec 30, 2008 7:58 pm
by Andrewrun
newphpnoob wrote:AWESOME! Thanks that solved it.

I just have some questions about the code it self;

What is the question mark mean? I did for a resaons just toatlly forgot it :oops:

Code: Select all

 
echo (!$echoed) ? '</div><div id="ccol-one">' : '';
 
I changed it to this;

Code: Select all

 
                    if ($i > $percol)
                    {
                         
                         if (!$echoed) 
                         {
                            echo    "</div><div id=\"ccol-one\">";
                         }
                         
                         $echoed = true;
                    }
 

Also is there a way where I can flip the order of the way how the data is being outputted? Becuase I want the stuff in the right column to be on the left column and vice a versa.
Sure, the question mark is just a shorthand if/then statement. It uses the form:

(test - Boolean true or false) ? code_to_execute_if_true : code_to_execute_if_false;

So in your case, $echoed is a boolean value. If it's true, the first part is outputted. If it's false, nothing is displayed.

Also, if you want to flip the data, you should just be able to switch the div id's. So everywhere you have div id=ccol-one, change it to id=ccol-two and vice versa.

EDIT: In order to just changing the id's work, you're going to have to clean up your CSS. I'll post something in a different post.