Jump to content

[Help]refactoring php codes


Diether

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

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

  On 3/11/2013 at 4:31 PM, AyKay47 said:

 

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

  On 3/11/2013 at 4:39 PM, Diether said:

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"

  On 3/11/2013 at 5:39 PM, ignace said:

 

Look at what you wrote, and THINK why it may not work.

 

Hint:

 

<?php
$hello = 'world';
echo "<?php echo $hello; ?>"; // Output: <?php echo world; ?>
?>

what line are you pertaining that it gives me this kind of result?

<?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 :)

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.