Print rowspan?

Let’ say i got 20 articles/items in my database. I use this code to print out the first 10 items:

    $result = mysqli_query($link, 'SELECT id,name,text FROM table ORDER BY date DESC LIMIT 10');
    if (!$result){
        $error = 'Error fetching articles: ' . mysqli_error($link);
        include 'error.html.php';
        exit();
    }
    while ($row = mysqli_fetch_array($result))
	{
        echo("<a href=\\"index.php?id=" . $row[0] . "\\">" . $row[1] . "</a> ");
    }


Below the list I want a link that points to the next 10 articles in the database. So I count the rows in the table:


$result=mysqli_query($link, "SELECT id,COUNT(id) FROM table");
 while ($row = mysqli_fetch_array($result))
 {
$test= $row['COUNT(id)'];

 }

… and throw in an IF statement:

if ($test>=10)

And here’s the problem. Tried this but it will only print out the same list as the first one:

if ($test>=10)
{

    $result = mysqli_query($link, 'SELECT id,name, text FROM table ORDER BY datum DESC LIMIT 10');
    while ($row = mysqli_fetch_array($result))
	{
        echo("<a href=\\"index.php?id=" . $row[0] . "\\">" . $row[1] . "</a> ") . "<br />";
    }
}

Where do I go from here?

I know that arrays start from zero, but the creation of the array is here:

$r = mysqli_fetch_row($result);

Right? If an array starts from zero, why say it once again in the statement below?

So I guess I am trying to understand how:

$r = mysqli_fetch_row($result);

is connected to:

$numrows = $r[0];

One question. How do the script knows what is the current page? I am thinking about this section:

if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
   // cast var as int
   $currentpage = (int) $_GET['currentpage'];
} else {
   // default page num
   $currentpage = 1; 

First I thought that there’s probably a php function called currentpage, but that does not seem to be true. So how do the script knows what is the current page?

like this is ok


echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1&archive=1'><<</a> ";

You write “i just assigned a value=1 as an example”. Which values should I use? This is the original link:

   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";

I can change it to:

   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1archive'><<</a> ";

But what should I write after “archive”? I guess that "&archive=1 " is not totally random.

ok thanks, how did you set the variables for $offset and $rowsperpage ?

$rowsperpage = 3;
$offset = ($currentpage - 1) * $rowsperpage;

…and?

Here’s my query:

$sql = "SELECT id, name FROM table LIMIT $offset, $rowsperpage";

It works if I dont put the code into an included file.

That I understand, but why the zero?

$r[0];

Thank you! I will check out the articles.

Have a quick look on Sitepoint for pagination. Rather than running what your doing which is essentially the same MySQL query, you need to add another factor to your limiting - think of it like this:

You have 67 articles (for arguments sake) in your database. You count them, using the count statement you showed us earlier. Now, work out how many articles you want - commonly, and what you are looking at: 10.
Therefore, with a bit of maths (and rounding up article number / page number) we reach 7 pages. We can then run a MySQL query, based on which page we are on of pagination, and LIMIT this, by setting two limits, like so:


SELECT id, number FROM numbers LIMIT $offset, $rowsperpage

Where offset is how many articles have gone by, and rowsperpage is your number, 10, which becomes:


SELECT id, number FROM numbers LIMIT 59, 10

Take a further look here: http://www.phpfreaks.com/tutorial/basic-pagination.

As stated earlier, Sitepoint have some articles on this too.

$x is variable


echo '<a href=' . $_SERVER['PHP_SELF'] . '?currentpage=' . $x .'>' . $x . '</a>';

ok thanks for trying out my suggestion razzel :slight_smile:

have you tried what adamcoppard said at the near beginning of this thread? that you need to limit the query accordingly


SELECT id, number FROM numbers LIMIT $offset, $rowsperpage

Does not work. I click the archive link and get this:

article 1
article 2
article 3

[1] 2 3 > >>

I then click the number 2 link:

http://localhost/index.php?currentpage=2

but gets the index.php instead of a listing of three new articles. It works if I dont put the code in an included file.

I have a follow-up question.

I tested the code at phpfreaks and it works just fine as long as I put it on the same php page. My webpage is built up in another way.
On the index.php I have a menu link i the navigation area, Archive, and if you click it it will include a file with the code from php builder. The page will show the first 10 items in the content area of the index page. I also get the pagination links which makes it possible to look at the next 10 items and so forth. The problem is that the pagination links dont work. If i click a pagination link it does not show me 10 more items, instead it will show the index.php page. I guess it is this that causes the problem:

 <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";

The problem seems to be related to the fact that I am using a php include. So that means that I have to rewrite the script?

ok thanks

can you please try out the code i posted and let me know if you still got some errors. we’ll try to figure it out more if same error exists


echo '<a href=' . $_SERVER['PHP_SELF'] . '?currentpage=' . $x .'>' . $x . '</a>'; 

Thank your for your tips, but they don’t work. The problem remains. Any other suggestion?

that is the total number of records you have

How do you interpret this (from the code)?

$numrows = $r[0];