Current location: Hot Scripts Forums » Programming Languages » PHP » PHP, MySQL Beginner... Help Needed Displaying Results


PHP, MySQL Beginner... Help Needed Displaying Results

Reply
  #1 (permalink)  
Old 05-10-04, 12:41 AM
db3db3 db3db3 is offline
New Member
 
Join Date: May 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
PHP, MySQL Beginner... Help Needed Displaying Results

Hi everyone...

I desperately need some help with the code below...

----------------------------------------------------

<table width="800">
<tr>
<td>
<?php

// open connection
$conn = mysql_connect("localhost", "my_username", "pass");

// pick database
mysql_select_db("my_database",$conn);

// sql statement
$sql = "SELECT * FROM mytable";

// execute the sql statement
$result = mysql_query($sql, $conn) or die(mysql_error());

// go through each row in the result set and display data
while ($newArray = mysql_fetch_array($result))

{

// give a name to each of the fields
$producturl = $newArray['ProductURL'];
$imageurl = $newArray['ImageURL'];
$title = $newArray['Title'];

//echo the results onscreen
echo "<a href=\"$producturl\"><img
src=\"$imageurl\" border=\"0\"></a><br><a
href=\"$producturl\">$title</a></td><td>";

}

?>
</tr>
</table>

----------------------------------------------------

Right now the code works as it should I imagine... It prints out one table row (probably 20 screens wide), with each product displayed in a cell.

What I'm trying to do is get the results to print out 4 products in a row (one per cell), with as many rows as are needed to display the results.

From what I've read it seems like I need to incorporate a COUNT function, and a few if else if statements, but I don't have a clue as to how I should write them out. This is all still very much a new language to me.

Any help would be greatly appreciated.

Don Bedford.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 05-12-04, 02:24 AM
ActionPoint ActionPoint is offline
Newbie Coder
 
Join Date: May 2004
Location: UK
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
greetings Don,

you'll need to change up a couple of lines in here relating to your database tables and queries but this'll put your results into a four-column format and paginate as necessary based on a set number of results per page as specified by $limit at the beginning. I figured that'd be better than a potentially infinately long page of results The limit's set to 8 right now so you'll get two rows of 4 unless you change it.

Hope this works for ya and good luck!

Code:
<table width="800">
<tr>
<td>

<?php

// open connection
$conn = mysql_connect("localhost", "my_username", "pass");

// pick database
mysql_select_db("my_database",$conn);

// bring out the results of our query and format them in rows *and* columns.
		
// set the number of results to display on each page... 4x2=8 			
if (!($limit)){
	$limit = 8;
} 
		
// check to see if we're on the first page of the results
if (!($page)){
	$page = 0;
} 

// in order to paginate the results we need to know in advance how many there are		
$numresults = mysql_query("SELECT * FROM yourTable WHERE column = 'value'"); 
$numrows = mysql_num_rows($numresults); 

// if there are no results, print a friendly message	
if ($numrows == 0){
	print("<td>There are currently no results for this query</td>\n
	</tr>\n</table>\n"); 
exit();
}		
		
// divide the number of results by the number displayed on each page, to get number of pages
$pages = intval($numrows/$limit); 
		
// if there are results left over, we need to add one more page
if ($numrows%$limit) {
	$pages++;					
} 						
		
$current = ($page/$limit) + 1; 			// calculate the current page number.
		
if (($pages < 1) || ($pages == 0)) {
	$total = 1;
} else {				// if $pages is less than one or equal to 0, total pages equals 1
	$total = $pages;
} 						// if not, total pages is the value of $pages
		
$first = $page + 1; 
		
//if this is not the last results page, last result = $page plus $limit
		
if (!((($page + $limit) / $limit) >= $pages) && $pages != 1) {
$last = $page + $limit;
} else {				// if this is the last results page, last result equals total number of results
$last = $numrows;
} 			
		
// below here can be changed if you want more or less columns etc
$numcols = 4; 				// how many columns we want on the page
$numcolsprinted = 0; 		// how many columns we have so far, do not change!
		
// get the results to be displayed in this set 
$query = "SELECT * FROM FROM yourTable WHERE column = 'value' LIMIT $page, $limit ";
$result = mysql_query($query, $mysql_link) or die (mysql_error());
		
// get each row
while ($row = mysql_fetch_array($result)) {
$producturl = $row["ProductURL"];
$imageurl = $row["ImageURL"];
$title = $row["Title"];

// we have to check to see if a row has been completed and if it has, start a new one
if ($numcolsprinted == $numcols) { 
print "</tr>\n<tr>\n"; 
$numcolsprinted = 0; 
} 		
		
// output a row of results with columns evenly spaced
print "	<td><a href=\"$producturl\"><img src=\"$imageurl\" border=\"0\"></a><br><a href=\"$producturl\">$title</a></td>\n"; 
		
// increment row counter 
$numcolsprinted++; 
		
} 										// end while loop, at this point we have all the results
		
// now we need to pad out the table with empty cells if there's a final row with
// fewer results than the number of columns we want to display
$colstobalance = $numcols - $numcolsprinted; 
		
for ($i=1; $i<=$colstobalance; $i++) { 
print "	<td> </td>\n
	</tr>"; 
		}
		
?>		
<tr>
	<td colspan="4"></td>
</tr>
<tr>
	<td colspan="4">
<?php
if ($page != 0) { 						// don't show back link if this is the first page
$back_page = $page - $limit;
echo("&nbsp;&nbsp;<a href=\"$PHP_SELF?query=".$query."&page=$back_page&limit=$limit\">previous</a>&nbsp;&nbsp;\n");
} else {
	print ("&nbsp;&nbsp;&nbsp;&nbsp;");
}		
		
$ppage = $limit*($i - 1);
print ("page $current of $pages");
		
if (!((($page+$limit) / $limit) >= $pages) && $pages != 1) { // if this isn't the last page show 'next' link
$next_page = $page + $limit;
echo("&nbsp;&nbsp;<a href=\"$PHP_SELF?query=".$query."&page=$next_page&limit=$limit\">next</a>&nbsp;&nbsp;\n");
} else {
print ("&nbsp;&nbsp;&nbsp;&nbsp;");
}		
?>
</td>
</tr>
</table>

Last edited by ActionPoint; 05-12-04 at 02:28 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP and MySQL ? rob2132 Hot Scripts Forum Questions, Suggestions and Feedback 4 08-29-08 03:22 AM
Apache, MySQL, PHP, PERL, Content Providers Needed! vbgunz Traffic Exchange 0 04-17-04 09:26 PM
PHP & MySql Help Needed mkmagu Job Offers & Assistance 2 12-04-03 04:39 PM
PHP And MYSQL Developer Needed CarBoffin PHP 2 10-13-03 04:31 AM


All times are GMT -5. The time now is 01:43 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.