Jump to content

[SOLVED] mysql_fetch_array question


affordit

Recommended Posts

Hello again

Im pulling pics and discriptions from DB and I got it so that it will get the first five records and display them horizontally accross the page but I can't figure out how to start a new row and have it start from where it left off in the DB. Here is what I am using now It counts the rows sets a limit and displays fine just can't get the next row to start at six.

// PROCESS CONTENTS OF $RESULTS

print "<table width='0' align='center'><tr>";
while ($info = mysql_fetch_array($result)) {
print "<td width='0' ALIGN='CENTER'>";
echo $info['heading'];
echo "<BR>";
echo $info['picture'];
echo "<BR>";
echo $info['description'];
echo"<BR></td>";
}
echo"</tr>";

Link to comment
https://forums.phpfreaks.com/topic/89389-solved-mysql_fetch_array-question/
Share on other sites

OK the way I understand pagination I can set the limit I got that but When I set the limit to 5 I get a row that looks good but when I increase the limit to 10 it trys to put them all in the same row so I guess I would need to change the way the results are processed. I think I would need to change something in the while loop but I am not sure. Here is all the code.

<?php

include("sharons_dbinfo.inc.php");
mysql_connect(mysql,$username,$password);
mysql_select_db(mydatabase) or die( "Unable to select database");

//Count rows
$query = "SELECT count(*) FROM content";
$result = mysql_query($query) or trigger_error("SQL", E_USER_ERROR);
$query_data = mysql_fetch_row($result);
$numrows = $query_data[0];

$page = ( isset( $_GET['page'] ) && is_numeric( $_GET['page'] ) ) ? $_GET['page'] : 1;

$query = "SELECT count(*) FROM `content`";
$result = mysql_query( $query );
$fetch = mysql_fetch_row( $result );
$numRows = $fetch[0];

$rowsPerPage = ( isset( $_GET['show'] ) && is_numeric( $_GET['show'] && $_GET['show'] >= 1 ) && $_GET['show'] <= 40 ) ? $_GET['show'] : 5;
$lastPage = ceil( $numRows / $rowsPerPage );

$page = (int)$page;
if( $page < 1 )
{
   $page = 1;
}
elseif( $page > $lastPage )
{
   $page = $lastPage;
}

if( $numRows > 0 ) $limit = 'LIMIT ' . ( $page - 1 ) * $rowsPerPage . ',' . $rowsPerPage;
else $limit = '';

//Query the DB
$query = "SELECT * FROM content $limit";
$result = mysql_query($query) or trigger_error("SQL", E_USER_ERROR);


// PROCESS CONTENTS OF $RESULTS

print "<table width='0' align='center'><tr>";
while ($info = mysql_fetch_array($result)) {
print "<td width='0' ALIGN='CENTER'>";
echo $info['heading'];
echo "<BR>";
echo $info['picture'];
echo "<BR>";
echo $info['description'];
echo"<BR></td>";
}
echo"</tr>";
$lastPage = (int)$lastPage;

if ( $page == 1 )
{
echo( 'FIRST PREVIOUS' );
}
else
{
$prevPage = $page - 1;
echo( '<a href="?page=1" class="first">FIRST</a><a href="?page=' . $prevPage . '">PREV</a>' );    
}

$totalPages = $lastPage;
$curentPage = $page;
if ( $totalPages < 8 )
{
$start = 1;
$end = $totalPages;
}
else
{
if ( $curentPage > 3 )
{
	if ( $totalPages - $curentPage < 3 ) $end = $totalPages;
	else $end = $curentPage + 3;
	$start = $end - 6;
}
else
{
	$start = 1;
	$end = 7;
}
}

echo( '<strong>Page:</strong> ' );

if ( $start > 1 ) echo ' ... ';
for ( $i = $start; $i <= $end; $i++ )
{
if ( $i == $curentPage ) echo( '<span style="font-weight:bold;">' . $i . '</span>' ); else echo ' <a href="?id=' . $_GET['id'] . '&page=' . $i . '" style="font-size:1.00em;">' . $i . '</a> ';
}
if ( $end < $totalPages ) echo ' ... ';
echo( "<strong> of {$totalPages}</strong>" );

if ($page == $lastPage)
{
echo( 'NEXT LAST');
}
else
{
$nextPage = $page + 1;
?>
    <a href="?page=<?php echo $nextPage; ?>">NEXT</a>
<a href="?page=<?php echo $lastPage; ?>">LAST</a>
<?php
}
?>

I would think that this should work but I think I wrote the if statement wrong?

// PROCESS CONTENTS OF $RESULTS
print "<table width='0' align='center'><tr>";
while ($info = mysql_fetch_array($result)) {
print "<td width='0' ALIGN='CENTER'>";
echo $info['item'];
echo $info['heading'];
echo "<BR>";
echo $info['picture'];
echo "<BR>";
echo $info['description'];
echo"<BR></td>";
if( $info['item'] = 5 ){
echo"</tr>";
}
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.