Page 1 of 1

Displaying sql data in two colums - loop?

Posted: Sat Aug 25, 2007 8:23 pm
by blade_922
hey guys,

I have a small piece of php code which reads data from my sql database.

It uses a while loop to display 10 rows. When its displaying the data onto my website it displays it all in one column all below each other.

What i want to do is to have it display with 5 rows and two columns. I've tried looping where $t=0 and t++ but i suck at that so im unsure how exactly the looping would work so it takes a new row when theres already 2 piecec of data on the row.



How can i achieve this?

Regards

Here issome code

Posted: Sat Aug 25, 2007 8:28 pm
by yacahuma
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Take a look

Code: Select all

function arrayToTable($arr,$cols)
{
  $arrcnt = count($arr);
        if ($arrcnt==0)   return '';
  $rows = ceil($arrcnt/$cols);
        $tableArr = array();
        for($i=0;$i<( ($rows*$cols) - $arrcnt);$i++)
          $tableArr[]='&nbsp;';
        $tableArr=array_merge($arr, $tableArr);
        $str = '';
        $line = '';
        $cnt=1;
  foreach($tableArr as $cell)
        {
                  $str .= "<td>$cell</td>";
                        if ($cnt++ == $cols)
                        {
                          $line .= "<tr>$str</tr>";
                                $str = '';
                                $cnt=1;
                        }       
          
        }       
        return '<table width="100%">' . $line . '</table>';
}

//example code 
$arr = array(4,5,6,7,7,87,"HELLO","BLLLA");
arrayToTable($arr,3);
All you have to do is put your data into an array and then call the function


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

oops

Posted: Sat Aug 25, 2007 8:29 pm
by yacahuma
in your case

arrayToTable($arr,2);//2 columns

Posted: Sat Aug 25, 2007 8:32 pm
by blade_922
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


hey yacahuma,

that sort of coding is way past my expertise.

Here is my code as is

Code: Select all

echo "	<img src=\"images/spacer2.gif\">
	<table align=\"center\" width=\"98%\" $tablereg>
  	<tr>
		<td width=\"100%\" $tdreg3><SPAN class=\"headertitle\">&nbsp;Latest Screenshots</SPAN></td>
			</tr>\n";

$latestfile = mysql_query("SELECT DISTINCT title,date,cat_id,title,timestamp,url from ccms_screenshots GROUP BY cat_id ORDER BY id desc limit 10");


	while($row = mysql_fetch_assoc($latestfile)) 
{



echo "	
		<td width=\"75%\" $tdreg><SPAN class=\"content\"><img src=\"images/folder.gif\" align=\"absmiddle\" hspace=\"4\" /><img border=\"0\" src=\"thumbnailer.php?image=" .$row['url'] ."&type=Media\" border=\"0\">$row[title]</SPAN></td></tr>\n";

	}




echo "	</table>
	<img src=\"images/spacer2.gif\">\n";

}

Im not sure where the code you posted would fit in here


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

just put your data into an array

Posted: Sat Aug 25, 2007 10:52 pm
by yacahuma
Hello


Get your data into an array first

Code: Select all

$mydata = array(); //create the data holder
 while($row = mysql_fetch_assoc($latestfile)) 
{
  //fill the array with your data
  $mydata[] = "<img src=\"images/folder.gif\" align=\"absmiddle\" hspace=\"4\" /><img border=\"0\" src=\"thumbnailer.php?image=" .$row['url'] ."&type=Media\" border=\"0\">" . $row[title];
}

//Now all your data in inside an array. 
echo arrayToTable($mydata,2);//this prints out the table

Posted: Sun Aug 26, 2007 9:50 am
by blade_922
hey, okay.



Is this correct so far?

Code: Select all

//===================================================
// DISPLAY LATEST SCREENSHOTS 
//-----------------------------------------------------------------

if (!$_REQUEST[action]) {

//===================================================
// 	DISPLAY LATEST SCREENS
//-----------------------------------------------------------------
	$divtitle = "$lang_categories";
	include "templates/$skin/divtitle.php";
//===================================================

echo "	<img src=\"images/spacer2.gif\">
	<table align=\"center\" width=\"98%\" $tablereg>
  	<tr>
		<td width=\"100%\" $tdreg3><SPAN class=\"headertitle\">&nbsp;Latest Screenshots</SPAN></td>
			</tr>\n";

$latestfile = mysql_query("SELECT DISTINCT title,date,cat_id,title,timestamp,url from ccms_screenshots GROUP BY cat_id ORDER BY id desc limit 10");


	  $mydata = array(); //create the data holder 
 while($row = mysql_fetch_assoc($latestfile)) 
{ 
  //fill the array with your data 
  $mydata[] = "<img src=\"images/folder.gif\" align=\"absmiddle\" hspace=\"4\" /><img border=\"0\" src=\"thumbnailer.php?image=" .$row['url'] ."&type=Media\" border=\"0\">" . $row[title]; 
} 

//Now all your data in inside an array. 
echo arrayToTable($mydata,2);//this prints out the table 



echo "	</table>
	<img src=\"images/spacer2.gif\">\n";

}

With this i get an error saying


Fatal error: Call to undefined function: arraytotable() in /home/gtcave/public_html/....

You forgot the function

Posted: Sun Aug 26, 2007 9:54 am
by yacahuma
remember to include the function I gave you as part of the file. Maybe at the top. That way it will be defined when is time to use it.

Re: You forgot the function

Posted: Sun Aug 26, 2007 9:59 am
by blade_922
yacahuma wrote:remember to include the function I gave you as part of the file. Maybe at the top. That way it will be defined when is time to use it.
Hey, im not really php illiterate. See the code i just posted, that is correct? i replaced my while code with your array code.

Now what code should i put right at the top?

Posted: Sun Aug 26, 2007 10:53 am
by blade_922
okay i manage to get it partially working. well it shows it in 2 columns fine.

Thank you :D

btw one thinf, it displays the page a little messed up. im going to try fix it now.