Solved

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

Posted on 2008-10-02
12
214 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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
 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP & MySql - Turn Select Query Results into a Glyphicon 3 28
mysql between clause 2 22
PHP Sessions in WordPress 8 30
javascript: add id amounts 5 46
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

786 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