Jump to content

if else problems


MsKazza

Recommended Posts

Hi there,

I was hoping someone could provide an insight as to why this is not working, it is not listing the products in the order they should be, but it is still listing all 6, just random.  any help much appreciated.  I would image there is a much easier way to do this i just don't know it.

      <?php $result_product = mysqli_query($con,"SELECT * FROM products WHERE NOT specials='0' AND product_publish='y'"); ?>				
 
<form id="specials" name="specials" action="" method="post">
<table >
		<?php while($row_product1 = mysqli_fetch_assoc($result_product)){
		if ($row_product1['specials'] = 1) { 
			echo "<tr><td>";
			echo $row_product1['product_code'];
			echo "</td><td>";
			echo $row_product1['product_name'];
			echo "</td><td><span class=\"glyphicon glyphicon-trash\"></td></tr>"; 
			} else { 
        	echo "<tr><td><input type=\"text\" id=\"speical1code\"></td><td>add special</td></tr>";
         } }?>

		<?php while($row_product2 = mysqli_fetch_array($result_product)){
		if ($row_product2['specials'] = 2) { 
			echo "<tr><td>";
			echo $row_product2['product_code'];
			echo "</td><td>";
			echo $row_product2['product_name'];
			echo "</td><td><span class=\"glyphicon glyphicon-trash\"></td></tr>"; 
			} else { 
        	echo "<tr><td><input type=\"text\" id=\"speical1code\"></td><td>add special</td></tr>";
         } }?>

		<?php while($row_product3 = mysqli_fetch_array($result_product)) {
		if ($row_product3['specials'] = 3) { 
			echo "<tr><td>";
			echo $row_product3['product_code'];
			echo "</td><td>";
			echo $row_product3['product_name'];
			echo "</td><td><span class=\"glyphicon glyphicon-trash\"></td></tr>"; 
			} else { 
        	echo "<tr><td><input type=\"text\" id=\"speical1code\"></td><td>add special</td></tr>";
         } }?>

		<?php while($row_product4 = mysqli_fetch_array($result_product)) {
		if ($row_product4['specials'] = 4) { 
			echo "<tr><td>";
			echo $row_product4['product_code'];
			echo "</td><td>";
			echo $row_product4['product_name'];
			echo "</td><td><span class=\"glyphicon glyphicon-trash\"></td></tr>"; 
			} else { 
        	echo "<tr><td><input type=\"text\" id=\"speical1code\"></td><td>add special</td></tr>";
         } }?>

		<?php while($row_product5 = mysqli_fetch_array($result_product)) {
		if ($row_product5['specials'] = 5) { 
			echo "<tr><td>";
			echo $row_product5['product_code'];
			echo "</td><td>";
			echo $row_product5['product_name'];
			echo "</td><td><span class=\"glyphicon glyphicon-trash\"></td></tr>"; 
			} else { 
        	echo "<tr><td><input type=\"text\" id=\"speical1code\"></td><td>add special</td></tr>";
         } }?>

		<?php while($row_product6 = mysqli_fetch_array($result_product)) {
		if ($row_product6['specials'] = "9") { 
			echo "<tr><td>";
			echo $row_product6['product_code'];
			echo "</td><td>";
			echo $row_product6['product_name'];
			echo "</td><td><span class=\"glyphicon glyphicon-trash\"></td></tr>"; 
			} else { 
        	echo "<tr><td><input type=\"text\" id=\"speical1code\"></td><td>add special</td></tr>";
         } }?>
        </table>
      </form>

Link to comment
Share on other sites

Thank you for your reply, i don't want them to be echo'd on a loop, i want to keep them in their own rows, so if there is no number 3, that row will become the text box, and a new number 3 will be chosen.  I was hoping someone could please help me get this to work properly displaying the rows 1 through 6 or displaying the textbox if associated number doesn't exist.

 

thanks

Link to comment
Share on other sites

MsKazza,

 

Are you saying there is only one record for each 'specials' value. I.e. there would only be one record for the value 1, one record for value 2, etc.? If so, you still need to follow Muddy_Funsters direction, but with some additional logic.

 

1. Create a query that sorts the records by the specials value

2. Create a variable to track the increment specials value

3. Create a loop to extract the results and compare the specials value from the result and the current record. Then determine whether to display the current record or an option to add a record.

 

The one thing that is not clear is what are the maximum possible records. If it is only 6 (or a specific value) then you can hard code to that. Else, you can always add one "add" option after the last record.

 

Here is one approach. This code is not tested

<?php
 
    //Create and run query - sorting by specials value
    $query = "SELECT specials, product_code, product_name
              FROM products
              WHERE specials>0 AND product_publish='y'
              ORDER BY specials";
    $result = mysqli_query($con, $query);
 
    //Create an array of 6 elements with index starting at 1 (with default FALSE values)
    $specials = array_fill(1, 6, FALSE);
 
    //Populate the valid specials from the result set
    while($row = mysqli_fetch_assoc($result)) {
        $specials[$row['specials']] = $row;
    }
 
    //Iterate through the array and generate the output
    $specialsOutput = '';
    foreach($specials as $special) {
        if($special) {
            $specialsOutput .= "<td>{$special['product_code']}</td>\n";
            $specialsOutput .= "<td>{$special['product_name']}</td>\n";
            $specialsOutput .= "<td><span class=\"glyphicon glyphicon-trash\"></td>\n";
            $specialsOutput .= "</tr>";
        } else {
            $specialsOutput .= "<tr><td><input type=\"text\" id=\"special1code\"></td><td>add special</td></tr>";
        }
    }
 
?>
 
<form id="specials" name="specials" action="" method="post">
    <table>
    <?php echo $specialsOutput; ?>
    </table>
</form>
Edited by Psycho
Link to comment
Share on other sites

 

<?php while($row_product1 = mysqli_fetch_assoc($result_product)){
every one of those lines is a loop, I was saying that by applying the order to the actual query you will only need one loop, not one for every value in specials.

if you want a better answer ask a better question:

 

Generaly include:

1>what are you trying to accomplish - in detail

2>what result to you get just now

 

Specifically for this question include:

1>what order are you trying to get the products in?

2>what is stored in specials if it's not 0,1,2,3,4,5 or 9 ?

3>are you expecting to get results from the database where the specials column does not have one of the values (1,2,3,4,5,9) and if so how are you expecting to handle it?

4>what is this form element that you are trying to add supposed to do?

Link to comment
Share on other sites

You could include a counter in the first solution and simply check if the row you are about to output matches the counter, the counter being the next expected value. If it is not a match, output the blank/empty row you want and then do the normal output of the currently fetched row. Of course if there are possible multiple missing items, this counter itself would have to be in a loop, inside your fetch loop.

Link to comment
Share on other sites

You could include a counter in the first solution and simply check if the row you are about to output matches the counter, the counter being the next expected value. If it is not a match, output the blank/empty row you want and then do the normal output of the currently fetched row. Of course if there are possible multiple missing items, this counter itself would have to be in a loop, inside your fetch loop.

 

 

I was going to do that, but after thinking about the "gaps" of 2 or more - decided it would likely be more complicated than generating a temporary array with all applicable indexes and then updating the values using the result set.

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.