Jump to content

2 issues: not all data is being pulled from MYSQL / Dont know how to tell PHP


slickboyj

Recommended Posts

I'm have 2 issues with this function I've wrote.

 

1. I have a counter that counts how records = dept_id and no matter which dept_id I choose, I'm minus one, the count.

example. It says I have (5) items but only display's (4)

 

2. I've been trying to wrap my head around this logic. How do I write out.

      If dept_id is set the run sql_query (a) if it isn't then run sql_query (b).  <-- so far I've got this part.

      If dept_id has a variable, display the records. But if dept_id doesn't exist then display " does not exist"  <----the part i'm having trouble with.

 

I hope the following code helps

 

<h3>Catergory</h3>
    <ul class="sidemenu">
      <li><a href="new_stuff_cat.php?dept_id=1000">******</a></li>
      <li><a href="new_stuff_cat.php?dept_id=1500">******</a></li>
      <li><a href="new_stuff_cat.php?dept_id=2000">******</a></li>
      <li><a href="new_stuff_cat.php?dept_id=2500">******</a></li>	
              <li><a href="new_stuff_cat.php?dept_id=3000">******</a></li>	
              <li><a href="new_stuff_cat.php?dept_id=3500">******</a></li>	
              <li><a href="new_stuff_cat.php?dept_id=3600">******</a></li>
              <li><a href="new_stuff_cat.php?dept_id=4000">******</a></li>
              <li><a href="new_stuff_cat.php?dept_id=4500">******</a></li>
              <li><a href="new_stuff_cat.php?dept_id=5000">******</a></li>
              <li><a href="new_stuff_cat.php?dept_id=5500">******</a></li>
              <li><a href="new_stuff_cat.php?dept_id=6000">******</a></li>
              <li><a href="new_stuff_cat.php?dept_id=6500">******</a></li>
              <li><a href="new_stuff_cat.php?dept_id=6600">******</a></li>
              <li><a href="new_stuff_cat.php?dept_id=7000">******</a></li>
              <li><a href="new_stuff_cat.php?dept_id=7500">******</a></li>
              <li><a href="new_stuff_cat.php?dept_id=8000">******</a></li>
              <li><a href="new_stuff_cat.php?dept_id=8500">******</a></li>
              <li><a href="new_stuff_cat.php?dept_id=9000">******</a></li>
              <li><a href="new_stuff_cat.php?dept_id=9500">******</a></li>
              <li><a href="new_stuff_cat.php?dept_id=9999">******</a></li>
		</ul>	
  	</div>
  <!-- end of left siide menu-->
<!--  Start of main ..middle.. content-->	
    <div id="main"> 
<?php

	  	product_cat_listing_a();
		?> 
	</div>
  </div> 

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



	function product_cat_listing_a(){

	 require("includes/******"); 

	 $conn = mysql_connect(SQL_HOST, SQL_USER, SQL_PASS)
		     or die ('Could not connect to MySQL. ' .mysql_error());
		 	 mysql_select_db(SQL_DB, $conn) or die (mysql_error());

	// get the dept_id being passed by the link
	$dept_id= $_REQUEST['dept_id'];

	  // if dept_id is NOT empty  or if dept_id has a variable run the sql query 'find everything that equals the DEPT_ID'
	  if(!empty($dept_id)){
		$sql = "SELECT * FROM products_2008 WHERE dept_id = '$dept_id' ORDER BY product_id LIMIT 0, 800";
	// If dept_id is EMPTY ' hasn't been set' then list everything in a random order
	}else{
		$sql = "SELECT * FROM products_2008 ORDER BY RAND() LIMIT 0, 800";
	}

		$result = mysql_query($sql) or die (mysql_error());
		$count = mysql_num_rows($result) or die (mysql_error());
		mysql_close();

		$row = mysql_fetch_array($result) or die (mysql_error());	

	//if $dept_id is set then display the results 
	  if(isset($row['dept_id'])){
		echo "<table class='product_list'>			
				<tr>
		 	   	<td colspan='2'>
				<br />
				<br />Department ID: $dept_id - ($count) items</td>
			   </tr>";
		// displaying the results of dept_id
		while($row = mysql_fetch_array($result)) {			
			$sku_number = $row['product_sku_number'];
			// set the path to the photos
			$product_photo = "buyers/images/products/".$row['product_photo']."";	
			$model_number = $row['product_model_number'];
			// get price and format it	 
			$number = $row['product_retail_price'];
			$formatted = number_format($number);


			 if (($i % 4) == 0) {
			   echo "<tr>";
			 }
			  echo "<td valign='top' id='top_line'>
						<table class='product_listings'>
							<tr>
								<td><img src='$product_photo' width='100' border='0'/></td>
							</tr>
							<tr>
								<td>								
									<a href='product_detail.php?id=".$row['product_id']."'><p class='title'><strong>".$row['product_name']."</strong></a>
									<p>SKU:$sku_number</p>
									<p>Price: $ $number</p> 
								</td>
							</tr>
						</table>
					</td>";
			 if (($i % 2) == 2) {

				echo "</tr>";
			 }
			  $i++;
			 }
			 echo "</tr>
			 </table>";

	 	 } else {	
	  		echo "<table class='product_list'>			
				<tr>
		 	   	<td colspan='2'>
				<br />
				<br />Department ID: $dept_id - ($count) items</td>
			   </tr>
			   <td valign='top' id='top_line'>
					<table class='product_listings'>
						<tr>
							<td>No Items to be displayed</td>
						</tr>
						<tr>
						    <td></td>
						</tr>
					</table>
				</td>
			</tr>
		</table>";
		}
	  }

 

 

 

Thanks for viewing.

Link to comment
Share on other sites

I'm have 2 issues with this function I've wrote.

 

1. I have a counter that counts how records = dept_id and no matter which dept_id I choose, I'm minus one, the count.

example. It says I have (5) items but only display's (4)

 

Thanks for viewing.

This is is due to the way you check to see if your query return any results, the following code is the problem

			
		$result = mysql_query($sql) or die (mysql_error());
		$count = mysql_num_rows($result) or die (mysql_error());
		mysql_close();

		$row = mysql_fetch_array($result) or die (mysql_error());	

	//if $dept_id is set then display the results 
	  if(isset($row['dept_id'])){

Each time you use mysql_fetch_array($result), it'll return a single row from the result set. As you have called mysql_fetch_array($result) this will return the first row from the result set, the when you get the while loop, only four results are displayed.

 

The following is an example of how to check if any results are return from a query:

$sql = 'YOUR QUERY';
$result = mysql_query($sql);

// check that any results was returned
// mysql_num_rows returns the number of rows returned from your query.
if(mysql_num_rows($result) > 0)
{
    // results returned, display results
    while($row = mysql_fetch_array($result))
    {
        // do whatever
    }
}
// no results returned
// display 'no results' message
else
{
    echo 'No results!';
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.