Advertisement

10.04.2008 at 04:52PM PDT, ID: 23787949
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.9

Pagination and Displaying records problem with mysql_fetch_row and mysql_fetch_array

Asked by Simon336697 in PHP Scripting Language

Hi everyone i hope you are all great.
Guys i have a bugging problem that i would really appreciate your help.
I have a situation where I cannot get both to display records and get pagination working together.
I can get them working independently of each other, which is pretty useless, but i cannot get them working together in the same file.
I have attached in the snippet 3 files:

1)  display_records_only.php   // only contains the display records code...which works
2)  pagination_code_only.php   // only contains the pagination code....which works.
3)  both_pagination_and_records.php // contains both 1) and 2) above, which errors.

In the above, when i run both_pagination_and_records.php, Im getting the following error:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\wamp\www\archive\lab\ee\002\both_pagination_and_records.php on line 135

Line 135 on my pc at home for this page is the following..

$rows=mysql_fetch_array($result);

This is line 301 in the code snippet.

The pagination component seems to work, but I cannot get any records to be displayed, due to this error i think.

Im not sure if it is because im using mysql_fetch_row to get a count of the number of rows in my query, and then a mysql_fetch_array later to display the results.

I would really love your help guys on this one, as at the moment, its pretty useless having pagination working if i cannot display the results.

As i said, both 1) and 2) above actually work independently, but when i combine the code for both into file: both_pagination_and_records.php, it keels over with that error message stated above.

Any help appreciated.





Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:
319:
320:
321:
==================================== pagination_code_only.php
 
<head>
<link rel="stylesheet" media="all" type="text/css" href="css/tasks.css" />
<link rel="stylesheet" media="all" type="text/css" href="css/main_page_styles.css" />
<script src="js/sorttable.js" type="text/javascript"></script>
<script src="js/showhide_div.js" type="text/javascript"></script>
<?php
include ("connectiondb.php");
$tbl_tasks="currenttasks_ctk";
$tbl_projects="projects_pro";
 
?>
</head>
 
<body>
<center><h3>Tasks Database</h3></center>
 
 
<?php
 
/* xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Pagination Section:
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx */
 
/*******************************************************************************
1: Get the required page number:
*******************************************************************************/
 
if (isset($_GET['pageno'])) {
	$pageno = $_GET['pageno'];	
} else {
	$pageno = 1;
}
echo "The current page number is: " . $pageno . "<br>";
 
/*******************************************************************************
2: Identify how many database rows are available:
This code will count how many rows will satisfy the current query.
*******************************************************************************/
 
$query="SELECT count(*) FROM `$tbl_tasks`, `$tbl_projects` where fk_id_pro_ctk = id_pro order by id_ctk asc";
$result = mysql_query($query);
$query_data = mysql_fetch_row($result);
$numrows = $query_data[0];
echo "The number of rows is: " . $numrows;
 
/*******************************************************************************
3: Calculate the number of the $lastpage:
This code uses the values in $rows_per_page (that you specify you want to show)
and $numofrows in order to identify the number of the last page.
*******************************************************************************/
 
$rows_per_page = 15;
$lastpage = ceil($numrows/$rows_per_page);
echo "The value of the last page in this case will be: " . $lastpage . "<br>";
 
/*******************************************************************************
4: Ensure that $pageno is within range:
This code checks that the value of $pageno is an integer between 1 and $lastpage
*******************************************************************************/
 
$pageno = (int)$pageno;
if ($pageno > $lastpage) {
	$pageno = $lastpage;
}
if ($pageno < 1) {
	$pageno = 1;
}
 
/*******************************************************************************
5: Construct LIMIT clause eg. How many rows to view for each page. eg. 10
This code will construct the LIMIT clause for the sql SELECT statement.
*******************************************************************************/
 
$limit = 'LIMIT' . ($pageno - 1) * $rows_per_page . ',' . $rows_per_page;
 
/*******************************************************************************
6. Issue the database query:
*******************************************************************************/
 
// This has been done already as below:
$query="SELECT * FROM `$tbl_tasks`, `$tbl_projects` where fk_id_pro_ctk = id_pro order by id_ctk asc $limit";
$result=mysql_query($query);
 
/*******************************************************************************
7. Construct pagination hyperlinks:
Finally, we must construct the hyperlinks which will allow the user to SELECT
other pages. We start with the links for any previous pages.
*******************************************************************************/
 
// a) If on page 1, it wont provide hyperlinks (yet), it will just echo FIRST PREV
 
if ($pageno == 1) {
      echo " FIRST PREV ";
} else {
      printf("<a href='%s'>FIRST</a>", "{$_SERVER['PHP_SELF']}?pageno=1");
      $prevpage = $pageno-1;
      printf("<a href='%s'>PREV</a>", "{$_SERVER['PHP_SELF']}?pageno={$prevpage}");
}
 
// b) We now inform the user of his current position in the sequence of available pages:
echo " ( Page $pageno of $lastpage ) ";
 
// c) This code will provide the links for any following pages:
 
if ($pageno == $lastpage) {
	echo " NEXT LAST ";
} else {
	$nextpage = $pageno+1;
	printf("<a href='%s'>NEXT</a>", "{$_SERVER['PHP_SELF']}?pageno=$nextpage");
	printf("<a href='%s'>LAST</a>", "{$_SERVER['PHP_SELF']}?pageno=$lastpage");
}
 
?>
 
 
 
 
==================================== display_records_only.php
<head>
<link rel="stylesheet" media="all" type="text/css" href="css/tasks.css" />
<link rel="stylesheet" media="all" type="text/css" href="css/main_page_styles.css" />
<script src="js/sorttable.js" type="text/javascript"></script>
<script src="js/showhide_div.js" type="text/javascript"></script>
<?php
include ("connectiondb.php");
$tbl_tasks="currenttasks_ctk";
$tbl_projects="projects_pro";
 
?>
</head>
 
<body>
<center><h3>Tasks Database</h3></center>
 
<!-- START OF DISPLAY RECORDS: on its own, the following displays all records no problem ############# -->
<?php
$query="SELECT * FROM `$tbl_tasks`, `$tbl_projects` where fk_id_pro_ctk = id_pro order by id_ctk asc $limit";
$result=mysql_query($query);
$numrows=mysql_num_rows($result);
?>
<table id="table_sort_all" class="sortable">
<thead><tr><td><b>ID</b></td><td><b>Task Name</b></td><td><b>Project</b></td><td><b>Description</b></td></tr></thead>	
<tbody>										
				
<?php
for ($i=0; $i < $numrows; $i++) {
$rows=mysql_fetch_array($result); // get a row from our result set
if($i % 2) { // if there is a remainder 
?>
<tr class="altRow_0">
 <?php
}
else { // if there is not a remainder we will do the else ?>
<tr class="altRow_1">
 <?php
} ?>						
<td id="viewtasksidall"><?php echo $rows['id_ctk']; ?></td>
<td id="viewtasksnameall"><?php echo stripslashes($rows['name_ctk']); ?></td>
<td id="viewtasksprojectall"><?php echo stripslashes($rows['name_pro']); ?></td>
<td id="viewtasksdescriptionall"><?php echo $rows['description_ctk']; ?></td>			
</tr> <?php } ?>					
</tbody>		
<tfoot></tfoot>		
</table> 
<!-- END OF DISPLAY RECORDS: ############# -->	
 
</body>
 
 
==================================== both_pagination_and_records.php
 
<head>
<link rel="stylesheet" media="all" type="text/css" href="css/tasks.css" />
<link rel="stylesheet" media="all" type="text/css" href="css/main_page_styles.css" />
<script src="js/sorttable.js" type="text/javascript"></script>
<script src="js/showhide_div.js" type="text/javascript"></script>
<?php
include ("connectiondb.php");
$tbl_tasks=	"currenttasks_ctk";
$tbl_projects="projects_pro";
?>
</head>
 
<body>
<center><h3>Tasks Database</h3></center>
 
 
<?php
 
/* xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Pagination Section:
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx */
 
/*******************************************************************************
1: Get the required page number:
*******************************************************************************/
 
if (isset($_GET['pageno'])) {
	$pageno = $_GET['pageno'];	
} else {
	$pageno = 1;
}
echo "The current page number is: " . $pageno . "<br>";
 
/*******************************************************************************
2: Identify how many database rows are available:
This code will count how many rows will satisfy the current query.
*******************************************************************************/
$query="SELECT count(*) FROM `$tbl_tasks`, `$tbl_projects` where fk_id_pro_ctk = id_pro order by id_ctk asc";
$result = mysql_query($query);
$query_data = mysql_fetch_row($result);
$numrows = $query_data[0];
echo "The number of rows is: " . $numrows;
 
/*******************************************************************************
3: Calculate the number of the $lastpage:
This code uses the values in $rows_per_page (that you specify you want to show)
and $numofrows in order to identify the number of the last page.
*******************************************************************************/
 
$rows_per_page = 15;
$lastpage = ceil($numrows/$rows_per_page);
 
echo "The value of the last page in this case will be: " . $lastpage . "<br>";
 
 
/*******************************************************************************
4: Ensure that $pageno is within range:
This code checks that the value of $pageno is an integer between 1 and $lastpage
*******************************************************************************/
 
$pageno = (int)$pageno;
if ($pageno > $lastpage) {
	$pageno = $lastpage;
}
if ($pageno < 1) {
	$pageno = 1;
}
 
/*******************************************************************************
5: Construct LIMIT clause eg. How many rows to view for each page. eg. 10
This code will construct the LIMIT clause for the sql SELECT statement.
*******************************************************************************/
 
$limit = 'LIMIT' . ($pageno - 1) * $rows_per_page . ',' . $rows_per_page;
 
/*******************************************************************************
6. Issue the database query:
*******************************************************************************/
 
// This has been done already as below:
$query="SELECT * FROM `$tbl_tasks`, `$tbl_projects` where fk_id_pro_ctk = id_pro order by id_ctk asc $limit";
$result=mysql_query($query);
 
/*******************************************************************************
7. Construct pagination hyperlinks:
Finally, we must construct the hyperlinks which will allow the user to SELECT
other pages. We start with the links for any previous pages.
*******************************************************************************/
 
// a) If on page 1, it wont provide hyperlinks (yet), it will just echo FIRST PREV
 
if ($pageno == 1) {
      echo " FIRST PREV ";
} else {
      printf("<a href='%s'>FIRST</a>", "{$_SERVER['PHP_SELF']}?pageno=1");
      $prevpage = $pageno-1;
      printf("<a href='%s'>PREV</a>", "{$_SERVER['PHP_SELF']}?pageno={$prevpage}");
}
 
// b) We now inform the user of his current position in the sequence of available pages:
echo " ( Page $pageno of $lastpage ) ";
 
// c) This code will provide the links for any following pages:
 
if ($pageno == $lastpage) {
	echo " NEXT LAST ";
} else {
	$nextpage = $pageno+1;
	printf("<a href='%s'>NEXT</a>", "{$_SERVER['PHP_SELF']}?pageno=$nextpage");
	printf("<a href='%s'>LAST</a>", "{$_SERVER['PHP_SELF']}?pageno=$lastpage");
}
 
?>
 
 
<!-- START OF DISPLAY RECORDS: on its own, the following displays all records no problem ############# -->
<?php
$query="SELECT * FROM `$tbl_tasks`, `$tbl_projects` where fk_id_pro_ctk = id_pro order by id_ctk asc $limit";
$result=mysql_query($query);
// $numrows=mysql_num_rows($result);
?>
<br>
<table id="table_sort_all" class="sortable">
<thead><tr><td><b>ID</b></td><td><b>Task Name</b></td><td><b>Project</b></td><td><b>Description</b></td></tr></thead>	
<tbody>					
<?php
for ($i=0; $i < $numrows; $i++) {
$rows=mysql_fetch_array($result); 
if($i % 2) { // if there is a remainder 
?>
<tr class="altRow_0">
<?php
}
else { // if there is not a remainder we will do the else ?>
<tr class="altRow_1">
<?php
}?>							
<td id="viewtasksidall"><?php echo $rows['id_ctk']; ?></td>
<td id="viewtasksnameall"><?php echo stripslashes($rows['name_ctk']); ?></td>
<td id="viewtasksprojectall"><?php echo stripslashes($rows['name_pro']); ?></td>
<td id="viewtasksdescriptionall"><?php echo $rows['description_ctk']; ?></td>			
</tr> <?php } ?>					
</tbody>		
<tfoot></tfoot>		
</table> 
<!-- END OF DISPLAY RECORDS: ############# -->	
 
</body>
[+][-]10.04.2008 at 05:46PM PDT, ID: 22642826

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]10.04.2008 at 05:49PM PDT, ID: 22642832

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10.04.2008 at 08:43PM PDT, ID: 22643326

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: PHP Scripting Language
Sign Up Now!
Solution Provided By: hielo
Participating Experts: 2
Solution Grade: A
 
 
[+][-]10.04.2008 at 09:25PM PDT, ID: 22643419

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10.04.2008 at 09:27PM PDT, ID: 22643421

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 - Hierarchy / EE_QW_2_20070628