Jump to content

Recommended Posts

Hello

Heres my code:

 

$query = "SELECT * FROM comments ORDER BY date DESC"; //get the customer list
$result = mysql_query($query); //perform the query

//display the customer list
while($row = mysql_fetch_array($result)){
     echo '<a href="test1.php?id='.$row['commentid'].'">'.$row['name'].'</a><BR>'; //create a link with the customers id as the $_GET['id']
}

 

What if I want 2 names per line.. how do i make it fetch all the rows displaying them 2 per line still in descending order?  Thank you for all your help.

Link to comment
https://forums.phpfreaks.com/topic/182104-fetching-rows-and-columns/
Share on other sites

set a variable to equal 0 then add to the variable for each while loop. then if its even number add the break<br> i dont know how to check if its even or odd i think there must be some function....

$variable = 0;
then do while statemnt{
$variable = $variable++;
if(Num % 2 == 0)
{
// It's even so you can  
echo "<br>";
}
else
{
// It's odd so do two per line(no line break)
}  
}end while

if you want to check for even and odd, you can do

if ($number % 2 == 0) { //even
}
else {} //odd

 

you could stick it in a function if you want, and just call it

<?php
function isEven($number){
return (bool)($number % 2) == 0;
}
echo var_dump(isEven(4))."<br /.>";
echo var_dump(isEven(5));
?>

 

output:

bool(true) 
bool(false)

 

The % is called modulus. It basically divides the two numbers (integer division) and returns the whole number remainder. For example, 15 % 6 would be 3, because the closest multiple of 6 is 12, and 15-12 (which is the remainder) is 3.

 

well heres the thing.. that was just an example code to make things easier..

 

I have an upload form where users upload pictures into the database and this page is the one that displays them you know?

 

theres 10 results per page.. i have it paginated.. but i want it to display 2 pictures per row and then go to the next.. how is this possible..

this should give you an example of how to do it

$array = array();
for ($i = 0; $i < 18; $i++){
$array[] = $i;
}
$midPoint = ceil(count($array) / 2);
echo $midPoint;
echo "<table>";
for($i = 0; $i < $midPoint; $i++){
echo "<tr>";
echo "<td>{$array[$i]}</td>";
echo "<td>";
if (isset($array[$i + $midPoint]))
	echo $array[$i+$midPoint];

echo "</td>";
echo "</tr>";
}

seems to work for even and odd numbers, but I haven't throughly tested it. It puts them in 2 columns only, won't do 3 or more.

 

What you would need to do is store the information you wanted from your query into an array, something like

$array = array()
while($row = mysql_fetch_xxx()){
$array[] = $row['name'];//if I just wanted the name
//or
$array[] = $row;//if I want the whole row

 

If you are just getting a single column, that could should work out of the box (just replace the array in the code with yours) If you are getting the multidimensional array, you would need to change it to something like

 

//assume array is multidimensional
$midPoint = ceil(count($array) / 2);
echo $midPoint;
echo "<table>";
for($i = 0; $i < $midPoint; $i++){
echo "<tr>";
echo "<td>".$array[$i]['name']."</td>";
echo "<td>";
if (isset($array[$i + $midPoint]))
	echo $array[$i+$midPoint]['name'];

echo "</td>";
echo "</tr>";
}

 

 

if the number variable ends in 5 (or divided by 5 evenly and isnt a multiple of ten)

then end the table and start the second table for the right (set of 5 image)

?

 

maybe set two tables of equal width inside one table of total width. like the first table would be made before the loop

and it would be width: 600

then the table with one colum and 5 rows would be width:300 and then the next table would be on the right of that table would be width: 300 so they would both fit inside the 600.

dont know exactly maybe the two tables have to be like 298 or something because of borders or something. that should world. then on 5, end the left table and start the right

<table width="600">
<tr><td>
<table width="300">
<?php 
$number = "0";
while($row = mysql_fetch_array($result)){$number = $number++;

     echo '<tr><td><a href="test1.php?id='.$row['commentid'].'">'.$row['name'].'</a></td></tr>'; //create a link with the customers id as the $_GET['id']
if($number == "5"){echo "</table><table width='300'>";}
}
?>
</table>
</td></tr></table>

Sorry, I am very new to this.. i am having trouble on how to replace my array with this one.. may you please help? heres the code that is working right now..

 

$query = "SELECT * FROM comments ORDER BY date DESC"; //get the customer list
$result = mysql_query($query); //perform the query

//display the customer list
while($row = mysql_fetch_array($result)){
     echo '<a href="test1.php?id='.$row['commentid'].'">'.$row['name'].'</a><BR>'; //create a link with the customers id as the $_GET['id']
}

 

Thank you so much for your help.

 

 

$query = "SELECT * FROM comments ORDER BY date DESC"; //get the customer list
$result = mysql_query($query); //perform the query

//display the customer list
$array = array()
while($row = mysql_fetch_array($result)){
     $array[] = $row;
}

$midPoint = ceil(count($array) / 2);
echo $midPoint;
echo "<table>";
for($i = 0; $i < $midPoint; $i++){
echo "<tr>";
echo '<td><a href="test1.php?id='.$array[$i]['commentid'].'">'.$array[$i]['name'].'</a></td';
echo "<td>";
if (isset($array[$i + $midPoint]))
	echo '<td><a href="test1.php?id='.$array[$i+$midPoint]['commentid'].'">'.$array[$i+$midPoint]['name'].'</a></td';

echo "</td>";
echo "</tr>";
}

 

untested, but should be about right

this code worked for me...

$query = "SELECT * FROM users"; //get the customer list
$result = mysql_query($query); //perform the query

//display the customer list
$array = array();
while($row = mysql_fetch_array($result)){
     $array[] = $row;
}
$midPoint = ceil(count($array) / 2);
echo "<table border=\"1\">";
for($i = 0; $i < $midPoint; $i++){
echo "<tr>";
echo '<td><a href="test1.php?id='.$array[$i]['username'].'">'.$array[$i]['username'].'</a></td';
echo "<td>";
if (isset($array[$i + $midPoint]))
	echo '<td><a href="test1.php?id='.$array[$i+$midPoint]['username'].'">'.$array[$i+$midPoint]['username'].'</a></td';

echo "</td>";
echo "</tr>";
}

 

it changed the columns to username, and it showed me a 2 column table..


three rows is 6 things right? u have to set a limit on your mysql query

to get 6 things at a time. this is the pagenator that works the best for me:

if (isset($_GET

))
{$page = mysql_real_escape_string($_GET

);
if($page <= 0)
{$page = 1;}}
else
{$page = 1;}
$limit = 6;<---that thing is the amount of things per page
$numpages = ceil($totalnoapps / $limit);
$start = $page * $limit;
$realstart = $start - $limit;
    if($page != 1)
{$prevs = $page - 1;
$startpage = $page;}
else
{$startpage = 1;}
for ($pix = $startpage; $pix <= $page+6; $pix++)
{if ($pix <= $numpages && $totalnoapps > $limit)
{if($pix == $page)
{ ?>
<a href="yourpage.php?page=<?php echo $prevs; ?>"><?php echo $prevs; ?></a>
<span style="color:white;font-family:arial narrow;font-size:55px; line-height:55pt;letter-spacing:-3px"><?php echo $pix; ?></span>
<?php } else{ ?>
<a href="yourpage.php?page=<?php echo $pix; ?>"><?php echo $pix; ?></a>
<?php }}}

$get = mysql_query("SELECT * FROM blah WHERE `whatever urusing` = '$whatever ur doing' ORDER BY `id` DESC LIMIT $realstart, $limit");
then the while statment goes here.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.