Displaying sql data in two colums - loop?
Moderator: General Moderators
Displaying sql data in two colums - loop?
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
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
feyd | Please use
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]
Take a lookCode: 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[]=' ';
$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);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]feyd | Please use
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]
hey yacahuma,
that sort of coding is way past my expertise.
Here is my code as isCode: Select all
echo " <img src=\"images/spacer2.gif\">
<table align=\"center\" width=\"98%\" $tablereg>
<tr>
<td width=\"100%\" $tdreg3><SPAN class=\"headertitle\"> 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
Hello
Get your data into an array first
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 tablehey, okay.
Is this correct so far?
With this i get an error saying
Fatal error: Call to undefined function: arraytotable() in /home/gtcave/public_html/....
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\"> 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
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
Hey, im not really php illiterate. See the code i just posted, that is correct? i replaced my while code with your array code.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.
Now what code should i put right at the top?