Page 1 of 1

multi column output

Posted: Fri Sep 17, 2004 6:34 am
by Think Pink
hey guys, I want to make a script that should disply info from $db in columns. Let's say 10 results / page.

i made a pagination script that display 10 results / page but in rows, not in columns. could you pls help me to modufy this script to be able to display info on 2 columns and on 5 rows.
Thx.


here is what I did.

Code: Select all

<?php
@include ('connect_to_db.php');

/* If current pag has a number, use it;
if not, set one! */
if(!isset($_GET['pag'])){ 
$pag = 1; 
} else { 
$pag = $_GET['pag']; 
} 

// Define the number of results per pag 
$max_results = 5; 

/* Figure out the limit for the query based 
on the current pag number. */
$from = (($pag * $max_results) - $max_results); 

						
// Select the fields from the appropriate tables 
$sql = "SELECT * FROM pics ORDER BY Id ASC LIMIT $from, $max_results";
$resursa = mysql_query($sql);

// Determine the number of records returned
$number = mysql_num_rows($resursa);

// Print the information 
print "<table cellpadding='1' cellspacing='2' width='100%' border='0'>
<tr>
<td class='bg01' width='125' valign='middle'>&nbsp;<span class='scris1'>ID</span></td>
<td class='bg01' valign='middle'>&nbsp;<span class='scris1'>Picture name</span></td></tr>";

for($i=0; $i<$number; $i++) {
$id = mysql_result($resursa, $i, "id");
$nume = mysql_result($resursa, $i, "nume");

/* print even-numbered rows with diferent 
background and create mouseeffect */

if ($i%2 == 0) {
print "<tr bgcolor='#dcdcdc' onMouseOver=this.bgColor='#ffe4b5' onMouseOut=this.bgColor='#dcdcdc'>";
} else {
print "<tr bgcolor='#dde7f5' onMouseOver=this.bgColor='#ffe4b5' onMouseOut=this.bgColor='#dde7f5'>";
}
print "	<td width='125'>&nbsp;<span class='scris2'>$id</span></td>
<td>&nbsp;<span class='scris2'>$nume</span></td></tr>";
} // and if
print "</table>";

// Total number of results in db:
$total_results = mysql_result(mysql_query("SELECT COUNT(id) FROM pics"),0);

// Total number of pics.
$total_pics = ceil($total_results / $max_results);

// Close the database connection
mysql_close();
?>
<table align="center" width="100%" cellspacing="0" cellpadding="0" border="0">
<tr>
<td width="50%" align="right">
<?php
// Previous
if($pag > 1){
$prev = ($pag - 1);
echo "<a href="".$_SERVER['PHP_SELF']."?pag=$prev" class='link2'>&laquo;</a>&nbsp;";
}
											
// pag numbers
for($i = 1; $i <= $total_pics; $i++){
if(($pag) == $i){
echo "$i&nbsp;";
} else {
echo "<a href="".$_SERVER['PHP_SELF']."?pag=$i" class='link2'>$i</a>&nbsp;";
}
}

// Next
if($pag < $total_pics){
$next = ($pag + 1);
echo "<a href="".$_SERVER['PHP_SELF']."?pag=$next" class='link2'>&raquo;&nbsp;</a>";
}
?>
</td>
</tr>
</table>

?>

Posted: Fri Sep 17, 2004 6:45 am
by dethron
just add <td>'s :)

in the for loop
- get two element
- display them
- iterate i by 2 $i=$i+2

Posted: Fri Sep 17, 2004 7:30 am
by Chriss2004
hello,
i would be interested in the solution too.
Could you (dethron) pls explain it. I'm very new(bie).

how am I supposed to
just add <td>'s

in the for loop
- get two element
- display them
- iterate i by 2 $i=$i+2
Pls
Thx

Posted: Fri Sep 17, 2004 9:05 am
by Draco_03
dethron wrote:[- iterate i by 2 $i=$i+2
You also could use modular (% 2).

and here's an exemple

Code: Select all

for ($i=0, $numrows=mysql_num_rows($result); $i&lt;$numrows; $i++)
{
$row = mysql_fetch_row($result);
echo("
&lt;tr&gt;
&lt;td&gt;$row&#1111;1]&lt;/td&gt;
&lt;/tr&gt;
");
}
just a quick exemple
for eceryrow i take all information (witht he line $row = mysql..) and then i open a column with one td in it, and I put 2nd field information off my database(since row[0] would be the first one).
then i'll do it again for every row i have in my db

Posted: Fri Sep 17, 2004 2:57 pm
by dethron
Sorry for being late to answer, i have experienced a connection error. And i haven't have access to the internet until now. I wrote a good explantion about the issue, but it was lost during this problem :(

Now, let me summarize thing. (since i have lost many time, i should go back my work immidiately)

Let say you gathered information from db to an array. I will create the array manually now.

Code: Select all

<?php
$arr = ["zero","one","two","three","four","five"];
?>
Followings will display the output in one column.

Code: Select all

<?php
echo "<table>";
for($i=0; $i<count($arr); $i++){
    echo "<tr><td>".$arr[$i]."</td></tr>";
}
echo "</table>";
?>
If we want to use 2 column to display the data, followin is the easiest way

Code: Select all

<?php
echo "<table>";
$cnt = count($arr);
for($i=0; $i<$cnt; $i=$i+2){
    echo "<tr><td>".$arr[$i]."</td>";
    if(($i+1)<=$cnt)
        echo "<td>".$arr[$i+1]."</td></tr>";
    else
        echo "<td>&nbsp;</td></tr>";
}
echo "</table>";
?>
And remember this is not the best way, it is the simplest one.
Trying to write it for unknown number of colums will be a good example.

Good Luck.

Posted: Fri Sep 17, 2004 3:11 pm
by feyd
I've posted several times how to do n number multi-column output:

viewtopic.php?t=25692
viewtopic.php?t=25105
viewtopic.php?t=23442
viewtopic.php?t=23418

multi column output

Posted: Fri Sep 17, 2004 5:16 pm
by Think Pink
thx guys for all your explanations.
What do you think about thi? I found something in a tutorial and modified it a little.

Code: Select all

<?php
@include ('conectare.php');
// If current pag has a number, use it, if not, set one!
if(!isset($_GET['pag'])){ 
$pag = 1; 
} else { 
$pag = $_GET['pag']; 
} 
// Define the number of results per page and columns number
$max_results = 4;
$cols = 2;
/* Figure out the limit for the query based 
on the current pag number. */
$from = (($pag * $max_results) - $max_results); 
$query = "SELECT * FROM pics ORDER BY Id ASC LIMIT $from, $max_results";
$result = mysql_query($query);
$number = mysql_num_rows($result);
// start printing the info
echo "<table align='center' cellpadding='4' cellspacing='4' border='0'>";
//changed this to a for loop so we can use the number of rows
for($i = 0; $i < $number; $i++) {
	$id = mysql_result($result, $i, "id");
	$nume = mysql_result($result, $i, "nume");
    if($i % $cols == 0) {
        //if there is no remainder, we want to start a new row
        echo "<TR bgColor='#dcdcdc'>";
	echo "<TD onMouseOver=this.bgColor='#ffe4b5' onMouseOut=this.bgColor='#dcdcdc'>";
                echo "<img src='uploads/$nume' border='0' alt=''></td>";
   if(($i % $cols) == ($cols - 1) || ($i + 1) == $number) {
	echo "</TR>";
    }
}
echo "</table>";
// Figure out the total number of results in DB:
$total_results = mysql_result(mysql_query("SELECT COUNT(Id) FROM pics"),0);
// Figure out the total number of pics.
$total_pics = ceil($total_results / $max_results);
// Close the database connection
mysql_close();
// create next, previous links
echo "<table align='center' width='100' cellspacing='2' cellpadding='2' border='0'><tr><td>";
// previous
if($pag > 1){
	$prev = ($pag - 1);
	echo "<a href="".$_SERVER['PHP_SELF']."?pag=$prev" class='link2'>Previous</a>&nbsp;";
}
echo "</td><td align='center'>";
// pag numbers
for($i = 1; $i <= $total_pics; $i++){
	if(($pag) == $i){
								echo "$i&nbsp;";
	} 
	else {
		echo "<a href="".$_SERVER['PHP_SELF']."?pag=$i" class='link2'>$i</a>&nbsp;";
	}
}
echo "</td><td align='right'>";
// next
if($pag < $total_pics){
	$next = ($pag + 1);
	echo "<a href="".$_SERVER['PHP_SELF']."?pag=$next" class='link2'>Next&nbsp;</a>";
}
echo "</td></tr></table>";
?>
Thx again

Posted: Fri Sep 17, 2004 6:20 pm
by dethron
feyd proposed a better way to do that, look at the links stated above.