Jump to content

help with sorting and displaying mysql results


digitalmartyr

Recommended Posts

Im trying to have a list of products for the user to view. in my index.php i have this code . .it contains the viewproducts.php, and has links to pass variables to the url.

index.php

<?php
session_start();
if(!$_SESSION['USERNAME'] == '')
{
$sessionUser = $_SESSION['USERNAME'];
$sessionLogged = $_SESSION['LOGGEDIN'];
}
else
{
header("Location: ../login.php?logout='yes'");
}

if($_POST['logout'])
{
session_destroy();
$logoutValue = 'yes';
header("Location: ../login.php?logout='yes'");
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Untitled Document</title>
<link rel="stylesheet" href="assets/styles/master.css" type="text/css" media="screen" title="Master" charset="utf-8" />
<!--[if IE 6]>
<link rel="stylesheet" href="assets/styles/ie6.css" type="text/css" media="screen" title="Master" charset="utf-8" />
<![endif]-->
</head>
<body>
<div id="container">

<div id="banner">
<h1><a href="index.php">Rock 925 - Est. 1990</a></h1>


</div><!--end of "banner"-->
<div id="sideNav">
<div id="searchBox">
<br /><br />
    	  <form action="search.php" method="GET" name="form1" target="_self" id="form1">
          <label>
          <input name="SearchString" type="text" id="SearchString" size="15" value="Enter Keywords" />
          </label>
          <input type="submit" name="Submit" id="submit" value="Search" align="baseline" />
          </form>
<br />
<center><hr /></center>
</div><!--end of "searchBox"-->
<div>
<h3>Site Management</h3>
<br /><br />
<ul>

<li><a href="index.php?page=addprod">Add Product</a></li>
<li><a href="index.php?page=addcat">Add Category</a></li>
<li><a href="index.php?page=viewall&cat=all">View all Products</a></li>
<li><a href="#">View by Category</a></li>
<li class="sublist"><a href="index.php?page=viewall&cat=pendants" class="subcategory">Pendants</a></li>
<li class="sublist"><a href="index.php?page=viewall&cat=chains" class="subcategory">Chains</a></li>
<li class="sublist"><a href="index.php?page=viewall&cat=earings" class="subcategory">Earings</a></li>
<li class="sublist"><a href="index.php?page=viewall&cat=rings" class="subcategory">Rings</a></li>
<li class="sublist"><a href="index.php?page=viewall&cat=misc" class="subcategory">Misc</a></li>
</ul>
<br />
<center><hr /></center>
</div><!--end of "categoryBox"-->
<div id="mailingBox">
<form class="mailingList" enctype="multipart/form-data" method="post" action="">
<h2>Join our Mailing List</h2>

<label class="desc" id="title0" for="Field0">
<span class="smText">Your Email Address</span>
		</label>

	<input id="Field0" 
		class="field text large"
		name="Field0" 
		tabindex="1"
            size="15"
            type="text" maxlength="255" value="" /> 


	<input id="saveForm" class="btTxt" type="submit" tabindex="2" value="Submit" />

</form>
</div><!--end "mailingBox"-->

</div><!--end of "sideNav"-->

<div id="topNav">
<ul>

</ul>
</div><!--end of "topNav"-->

<div id="mainContent">
<div class="box">
<div class="box-outer">

<div id="varContent">

<!------------------------------------------------ DO NOT CHANGE ANYTHING ABOVE THIS LINE!!  --->
<!------------------------------------------------ **START** EDITABLE CONTENT BELOW!!  --->
<div id="userInfo">
<?php
if(!isset($sessionUser) =='')
{
echo "<span>Welcome back ".$sessionUser."</span>";
echo "<br />";
}
if(!isset($sessionLogged) =='')
{
echo "<span>Logged in at ".$sessionLogged."</span>";
echo "<br />";
}
?>
<a href='<?php echo "../login.php?logout=yes";?>' >Logout</a>
</div><!--end of userInfo -->
<div id='dynamicContent'>
<?php
$page = $_GET['page'];
switch($page){

case "addprod":
$content = "addproduct.php";
break;

case "viewall":
$content = "viewproducts.php";
break;

default:
$content = "addproduct.php";
break;
}
include($content);
?>
</div>
<!------------------------------------------------ **END** EDITABLE CONTENT!!  --->
<!------------------------------------------------ DO NOT CHANGE ANYTHING BELOW THIS LINE!!  --->
</div>

</div>
</div>

</div><!--end of "mainContent"-->

<div id="footer">
<br /><br /><br /><br />
<br /><br /><br /><br />

<p align="center">©2008 Rock9Twenty5.com  All Rights Reserved.</p>
<br /><br /><br /><br />

</div><!--end of "container"-->

</body>
</html>

 

 

and here is the viewproducts.php

 

<?php
require('dbconnect.php');
//
if($_GET['page'])
{
$pagesection = $_GET['page'];
}
if($_GET['cat'])
{
$prodCat = $_GET['cat'];
echo $prodCat;
}
//

if(!(isset($pagenum)))
{
//no page number default to 1
$pagenum = 1;
}
//get sql results and count
//see what cat is called
if($prodCat == 'all')
{
$data = mysql_query("SELECT * FROM products");	
}
else
{
$data = mysql_query("SELECT product_name, product_price, product_dscrb, product_img FROM products WHERE category = '$prodCat'");
}
//
$rows = mysql_num_rows($data);
//number of results per page
$page_rows = 3;

//tells the page number of our last page
$last = ceil($rows/$page_rows);
//make sure page number isnt below 1 or more than maxpages
if($pagenum < 1)
{
$pagenum = 1;
}
elseif ($pagenum > $last)
{
$pagenum = $last;
}

//sets the range to display in our query
$max = ' LIMIT '.($pagenum-1) * $page_rows.','.$page_rows;
$maxquery = $data.$max;
//run query to display results *style for display* 
//add $max to it for limiting result
$data_p = mysql_query($maxquery)or die(mysql_error());

while($info = mysql_fetch_array($data_p))
{
echo '<div id="productThumb">';
echo '<h3>product id: '.$info['product_id'].'</h3>';
echo '<h2>product name: '.$info['product_name'].'</h2>';
echo '<img height="140" width="100" src="'.$info['product_img'].'"</img>';
echo '<span>Product Describtion '.$info['product_dscrb'].'</span>';
echo '</div>';

}
//shows user what page they are on and the total number of pages
echo "<p>--page $pagenum of $last--<p>";

//check if on page 1, if so we dont need a link to the prev page or first page so do nothing
//if we arent on page 1 we generate links to the first page and to the prev pages
if($pagenum == 1)
{

}
else
{
echo "<a href='{$_SERVER['PHP_SELF']}?page=$pagesection&pagenum=1'>
<<-First</a>";
echo " ";
$previous = $pagenum-1;
echo "<a href='{$_SERVER['PHP_SELF']}?page=$pagesection&pagenum=$previous'>
<-Previous</a>";
}

//this does the same above, only checking to see if we are on the last page
//and then generating the NExt and Last link
if($pagenum == $last)
{

}
else
{
$next = $pagenum+1;
echo "<a href='{$_SERVER['PHP_SELF']}?page=$pagesection&pagenum=$next'>Next-></a>";
echo " ";
echo "<a href='{$_SERVER['PHP_SELF']}?page=$pagesection&pagenum=$last'>Last->></a>";
}
?>

i have it writing a sql query which is dependent on the cat variable set by the link in index.php... i think something is wrong with the way im executing the query to mysql. i have some other code in there for pagination as well.... note: i was able to get all the entries out of the table, but when i try and modify the code so they sort, it breaks.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.