Solved

Displaying mysql records on a page.....some skewed version of pagination

Posted on 2008-10-02
12
211 Views
Last Modified: 2011-10-19
Hi guys, I hope you are all well and can help.
I hope this is not a really strange request/question, but ill pose it to you anyway to see if you guys can help me.

##########
Scenario:
##########
I have a mysql table that I want to display all records from using PHP.
The number of records will mean that it is ideal to span the display of them on separate pages, since otherwise ill have 500 records on one page.
My question is this......

My table design is only 4 fields wide. Let's say they are:
ID, Task Name, Project, Description

Is it possible to do the following?

1) Show 50 records per page
2) On each "page", split it into 2 virtual columns, so that each virtual column displays 25 records in it.

An example would be the following of what im looking for:

===================================================================== Page 1

                <---- Virtual Column 1 ---->                   ||                   <------ Virtual Column 2 --->
ID      Task Name    Project      Description        ||     ID    Task Name   Project   Description
row1                                                                         ||     row26
row2                                                                         ||     row27
row3                                                                         ||     row28
........                                                                          ||     ..........
row25                                                                       ||      row50

                           << previous   1    2    3    4    5   next  >>

=============================================================== end of page 1

The

                           << previous   1    2    3    4    5   next  >>

would be the pagination part where clicking on for example, '2', would take you to page 2,
which would have again, 2 virtual columns, each one having 25 records.

Here is my current sql, that simply displays the records on one page only.

$sql_showall_tasks_all_projects="SELECT * FROM `$tbl_tasks`, `$tbl_projects` where fk_id_pro_ctk = id_pro";
$result_showall_tasks_all_projects=mysql_query($sql_showall_tasks_all_projects);

Any help on this greatly appreciated.
0
Comment
Question by:Simon336697
  • 6
  • 6
12 Comments
 
LVL 4

Expert Comment

by:kingmonty
ID: 22631663
You would need to fetch 50 results at a time with a LIMIT clause in MySQL:

$qry = "SELECT ID, Task Name, Project, Description FROM tasksTable [WHERE CLAUSE] LIMIT [offset],50";

You PHP script will handle the pagination. You can display the info however you wish,

example:


$cntr = 0;
<span id="leftColumn">
display the first 25
</span>
<span id="rightColumn">
display the next 25
</span>
0
 
LVL 1

Author Comment

by:Simon336697
ID: 22631811
Hi king, thank you so much for your help mate.
Im working on it now, and if you dont mind, will report back shortly.
0
 
LVL 1

Author Comment

by:Simon336697
ID: 22631874
Hi king, I have the following mate...

$sql_showall_tasks_all_projects="SELECT * FROM `$tbl_tasks`, `$tbl_projects` where fk_id_pro_ctk = id_pro order by id_ctk asc limit 50 offset 0";

Please see below :>)

With..

$cntr = 0;
<span id="leftColumn">
display the first 25      <----------- Im not sure how to get the first 25 here
</span>
<span id="rightColumn">
display the next 25 <------------------------ the next 25 here?
</span>




0
 
LVL 4

Expert Comment

by:kingmonty
ID: 22631949
Basically you'd need to do a fetch_row to get rows,

so the following code serves as an example:

NOTE, this is not tested, it's just a guide.

<?php

/* snippet */

if (!$offset)

	$offset = 0;
 

$sql_showall_tasks_all_projects = "SELECT * FROM `$tbl_tasks`, `$tbl_projects` where fk_id_pro_ctk = id_pro order by id_ctk asc limit $offset,50";

$res_showall_tasks_all_projects = mysql_query($sql_showall_tasks_all_projects);
 

$total_rows = mysql_num_rows();

$cntr = 1;

while ($row_showall_tasks_all_projects = mysql_fetch_row($query))

{

	if ($cntr == 1 || $cntr == 26)

    {

		echo '<span class="PageColumn">';

        echo "ID, Task Name, Project, Description";

	}

    

    echo "<span class='coltype'>$row_showall_tasks_all_projects[0]"</span>;

    echo "<span class='coltype'>$row_showall_tasks_all_projects[1]"</span>;

    echo "<span class='coltype'>$row_showall_tasks_all_projects[2]"</span>;

    echo "<span class='coltype'>$row_showall_tasks_all_projects[3]"</span>;

    

    if ($cntr == 25 || $cntr == 50 || $cntr == $total_rows)

	    echo '</span>';

        

	$cntr++;   

}
 

// pagination
 

/* end snippet */

?>

Open in new window

0
 
LVL 4

Expert Comment

by:kingmonty
ID: 22631957
The line

      while ($row_showall_tasks_all_projects = mysql_fetch_row($query))

should be:

     while ($row_showall_tasks_all_projects = mysql_fetch_row($res_showall_tasks_all_projects))

0
 
LVL 1

Author Comment

by:Simon336697
ID: 22632022
thank u so very much :>)
will test...
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 1

Author Comment

by:Simon336697
ID: 22632090
Monty mate...
here is a printscreen of the output i get using the attached code snippet.
Im getting an error for mysql_num_rows, which in my code is the following..

$total_rows = mysql_num_rows();

Where you have the span class, is there any reason why you have put 4 rows for this mate?

And does each span row belong to a different coltype class?


<?php 
 

if (!$offset)

	$offset = 0;

 

$sql_showall_tasks_all_projects = "SELECT * FROM `$tbl_tasks`, `$tbl_projects` where fk_id_pro_ctk = id_pro order by id_ctk asc limit $offset,50";

$res_showall_tasks_all_projects = mysql_query($sql_showall_tasks_all_projects);

 

$total_rows = mysql_num_rows();

$cntr = 1;

while ($row_showall_tasks_all_projects = mysql_fetch_row($res_showall_tasks_all_projects)) {
 

	if ($cntr == 1 || $cntr == 26)

    {

		echo '<span class="PageColumn">';

        echo "ID, Task Name, Project, Description";

	}

    

    echo "<span class='coltype'>$row_showall_tasks_all_projects[0]</span>";

    echo "<span class='coltype'>$row_showall_tasks_all_projects[1]</span>";

    echo "<span class='coltype'>$row_showall_tasks_all_projects[2]</span>";

    echo "<span class='coltype'>$row_showall_tasks_all_projects[3]</span>";

    

    if ($cntr == 25 || $cntr == 50 || $cntr == $total_rows)

	    echo '</span>';

        

	$cntr++;   

}
 

?>		

Open in new window

monty.jpg
0
 
LVL 4

Accepted Solution

by:
kingmonty earned 500 total points
ID: 22632167
Hi Simon

You'd need to actually style your Spans using CSS to get them into neat tables. Otherwise you can just use HTML tables, but that is often frowned upon nowadays.

Replace $total_rows = mysql_num_rows(); with this:

$total_rows = mysql_num_rows($res_showall_tasks_all_projects);


I've attached a HTML tables version...

Disclaimer: Once again, this is untested code.




if (!$offset)

	$offset = 0;
 

$sql_showall_tasks_all_projects = "SELECT * FROM `$tbl_tasks`, `$tbl_projects` where fk_id_pro_ctk = id_pro order by id_ctk asc limit $offset,50";

$res_showall_tasks_all_projects = mysql_query($sql_showall_tasks_all_projects);
 

$total_rows = mysql_num_rows($res_showall_tasks_all_projects);

$cntr = 1;

echo "<table width='100%' border='1'>

	<tr>";

while ($row_showall_tasks_all_projects = mysql_fetch_row($res_showall_tasks_all_projects))

{

	if ($cntr == 1 || $cntr == 26)

    {

		echo '<td>';

        echo "<table><tr><td>ID</td><td>Task Name</td><td>Project</td><td>Description</td></tr>";

	}

    

    echo "<tr><td>$row_showall_tasks_all_projects[0]</td>";

    echo "<td>$row_showall_tasks_all_projects[1]</td>";

    echo "<td>$row_showall_tasks_all_projects[2]</td>";

    echo "<td>$row_showall_tasks_all_projects[3]</td>";

    

    if ($cntr == 25 || $cntr == 50 || $cntr == $total_rows)

	    echo '</table></td>';

        

	$cntr++;   

}

echo "

	</tr>

</table>";
 

// pagination

Open in new window

0
 
LVL 4

Expert Comment

by:kingmonty
ID: 22632173
Oh, replace this line:

echo "<td>$row_showall_tasks_all_projects[3]</td>";


with

echo "<td>$row_showall_tasks_all_projects[3]</td></tr>";

0
 
LVL 1

Author Comment

by:Simon336697
ID: 22632892
King...

=============THAT LOOKS BRILLIANT THANK YOU SO MUCH THAT IS GREAT!!!!!!!!!!!!!! ============

Here is the output.
No more questions on this one for your kind kind self............fantastic.
I will study what you have done and try and fully understand it more.
I cant thank you enough.
monty.jpg
0
 
LVL 4

Expert Comment

by:kingmonty
ID: 22633004
Good stuff. Glad to help.
0
 
LVL 1

Author Closing Comment

by:Simon336697
ID: 31502670
Hi KMonty,
Thank you km for spending your time helping me. It is people like you I really respect in helping others, and you have certainly helped me.
Take care and thank you so much again. I wish you nothing but the best :>)
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now