Jump to content

Recommended Posts

Hi guys Good day, this codes works perfectly for me.

<?php

// This block grabs the whole list for viewing

$product_list = "";

$sql = mysql_query("SELECT * FROM product

           ORDER BY date_added DESC");

$productCount = mysql_num_rows($sql); // count the output amount

if ($productCount > 0) {

  while($row = mysql_fetch_array($sql)){

             $id = $row["id"];

       $product_name = $row["name"];

       $details = $row["details"];

       $size = $row["size"];

       $price = $row["price"];

       $quantity = $row["quantity"];

       $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));

       $product_list .= "Product ID: $id

                           - <strong>$product_name</strong> -

                        Php$price -

                        <em>Added $date_added</em>

                        <a href='inventory_edit.php?pid=$id'>edit</a> •

                        <a href='inventory_list.php?deleteid=$id'>delete</a><br />";

        }

      } else {

        $product_list = "You have no products listed in your store yet";

      }



?>


My problem is i want to refactor this part and put it into table in order to give a pleasant look:

    $product_list .= "Product ID: $id

                           - <strong>$product_name</strong> -

                        Php$price -

                        <em>Added $date_added</em>

                        <a href='inventory_edit.php?pid=$id'>edit</a> •

                        <a href='inventory_list.php?deleteid=$id'>delete</a><br />";


My Screenshots for my desired output.
1741996659880843c6a2ae94919531384505f7a1

Here is what i did to refactor but it did not give me a good result:

 $product_list .= "?>

                                 <html>

                                 <table >

                                    <tr>

                                        <td>

                                            Product ID: <?php echo $id ?>

                                        </td>

                                        <td>

                                              <?php echo $product_name; ?>

                                        </td>

                                        <td>

                                              Php <?php echo $price; ?>  

                                        </td>

                                        <td>

                                             Added <?php echo $date_added; ?>

                                        </td>

                                        <td>

                                             <?php echo <a href='inventory_edit.php?pid=$id'>edit</a> • ?>

                                        </td>

                                        <td>

                                            <?php echo <a href='inventory_list.php?deleteid=$id'>delete</a><br />"; ?>

                                        </td>

                                    </tr>

                                 

                                

                                

                                 </table>


pls help me to solve my desired output,, thanks

Edited by Diether
Link to comment
https://forums.phpfreaks.com/topic/275518-helprefactoring-php-codes/
Share on other sites

 

This is pseudo code to get you started with the basic structure.

 

 

<table>
  <?php
  while($row = mysql_fetch_array($result))
  {
    echo "<tr><td>something</td><td>something else</td></tr>";
  }
  ?>
</table>

 

this code does not follow my desired output. any solution guys.? please help me to solve this

Edited by Diether

this code does not follow my desired output. any solution guys.? please help me to solve this

 

You're right it does't, because it was meant to point out a few things that you are doing incorrectly.

 

1. Your attempt will output an entire html page/table every iteration; you want to generate rows within the table, thus the while loop belongs inside of the table

 

2. As Ignace pointed out, you are including <?php tags and functions inside of an echo statement, this is incorrect and will output the literal string "<?php echo"

Edited by AyKay47
<?php 
// This block grabs the whole list for viewing
$product_list = "";
$sql = mysql_query("SELECT * FROM product 
           ORDER BY date_added DESC");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
  $product_list .= "<table><tr><td>Product ID</td><td>Items</td><td>Price</td><td>Date</td><td>Action</td></tr>";
  
  while($row = mysql_fetch_array($sql)){
             $id = $row["id"];
       $product_name = $row["name"];
       $details = $row["details"];
       $size = $row["size"];
       $price = $row["price"];
       $quantity = $row["quantity"];
       $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
       $product_list .= "<tr><td>$id</td> 
	   					<td><strong>$product_name</strong></td>
						<td>Php$price</td>
						<td><em>$date_added</em></td>
            		    <td><a href='inventory_edit.php?pid=$id'>edit</a> •
	                    <a href='inventory_list.php?deleteid=$id'>delete</a><br /></td></tr>";
        }
      } else {
        $product_list = "<tr><td colspan=5>You have no products listed in your store yet</td></tr>";
      }

?>

this solve my problem a while ago.. hope it will help a beginner like me :)

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.