Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 231
  • Last Modified:

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

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
Simon336697
Asked:
Simon336697
  • 6
  • 6
1 Solution
 
kingmontyCommented:
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
 
Simon336697Author Commented:
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
 
Simon336697Author Commented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
kingmontyCommented:
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
 
kingmontyCommented:
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
 
Simon336697Author Commented:
thank u so very much :>)
will test...
0
 
Simon336697Author Commented:
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
 
kingmontyCommented:
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
 
kingmontyCommented:
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
 
Simon336697Author Commented:
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
 
kingmontyCommented:
Good stuff. Glad to help.
0
 
Simon336697Author Commented:
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now