Transform a string String into an HTML Table

0

I am creating a system where you build products and generate tables and budgets. You must implement a functionality to finish and if the user leaves the item's constructor but wants to return from another area of the website, for example, check out and wish to return to the builder must have everything that had done before the tables, prices etc. I did this by creating a temporary bbdd where the items are entered, so that the information is not lost. The detail is that the values that the bbdd returns to me must be transformed into tables and cells so that it remains exactly the same as the user had left it. replace, but I can not do it, is this possible?

This is what the temporary information creates

<?php
class temporalCarts{
    private $CART_ITEMS;
    private $PRICE_CART = 0;
    private $BBDD;
    public function __construct() {
         require_once 'config.php';
        try{
            $this->BBDD = new PDO(PDO_HOSTNAME, PDO_USER, PDO_PASS);
            $this->BBDD->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $this->BBDD->exec(PDO_CHAR);            
        } catch (PDOException $ex) {
            die("Fail to open Stream with the database server" . $ex->getLine() . PHP_EOL . $ex->getCode() . " " . PHP_EOL. $ex->getMessage());
        }      
    }
    public function setCartItem($CART_PRODUCTS){
        $this->CART_ITEMS = $CART_PRODUCTS;
    }
    public function getCartItem(){
        return $this->CART_ITEMS;
    }
    public function setCartPrice($PRICE_TMP){
        $this->PRICE_CART = $PRICE_TMP;
    }
    public function getCartPrice(){
        return $this->PRICE_CART;
    }
    public function addTmpCart($CART_, $PRICE){
        try{
            $statement = "INSERT INTO tmp_products (tmp_product, tmp_price) VALUES (:tmp_product, :tmp_price)";
            $INSERT_TMP = $this->BBDD->prepare($statement);
            $INSERT_TMP->bindValue(":tmp_product", htmlentities(addslashes($CART_)));
            $INSERT_TMP->bindValue(":tmp_price", htmlentities(addslashes($PRICE)));
            $INSERT_TMP->execute();
        } catch (PDOException $ex) {
            die("Fail to insert products in the server" . $ex->getLine() . PHP_EOL . $ex->getCode() . " " . PHP_EOL. $ex->getMessage());
        }
    }
    public function getTmpCart($CART_ITEM, $CART_PRICE){
        try{
            $statement = "SELECT * FROM tmp_products WHERE tmp_product = :product AND tmp_price = :price LIMIT 1";
            $TMP_CART_GET = $this->BBDD->prepare($statement);
            $TMP_CART_GET->bindValue(":product", htmlentities(addslashes($CART_ITEM)));
            $TMP_CART_GET->bindValue(":price", htmlentities(addslashes($CART_PRICE)));
            $TMP_CART_GET->execute();
            if($TMP_CART_GET->rowCount()!=0){
                return $TMP_CART_GET->fetchAll(PDO::FETCH_OBJ);
            }else{
                return NULL;
            }
        } catch (PDOException $ex) {
            die("Fail to read data from this cart" . $ex->getLine() . PHP_EOL . $ex->getCode() . " " . PHP_EOL. $ex->getMessage());
        }
    }
    
}

I invoke the class and pass the parameters to it.

 <?php
        if(isset($_GET["item"]) && (!empty($_GET["item"])) && ($_GET["item"]!="")
        && isset($_GET["PRICE_"]) && (!empty($_GET["PRICE_"])) && ($_GET["PRICE_"]!="")){
        $TMP_STRING1 = base64_decode($_GET["item"]);
        $TMP_PRICE = base64_decode($_GET["PRICE_"]);
        $TMP_REPLACE = str_replace("Remove","",$TMP_STRING1);
        $TMP_REPLACE2 = str_replace("-",",",$TMP_REPLACE);        
        require_once 'admin/tmp_cart.php';
        $cart_tmp = new temporalCarts();
        // setters
        $cart_tmp->setCartItem($TMP_REPLACE2);
        $cart_tmp->setCartPrice($TMP_PRICE);
        $cart_tmp->addTmpCart($cart_tmp->getCartItem(), $cart_tmp->getCartPrice());
        // READ PARAM
        $cart_read = $cart_tmp->getTmpCart($cart_tmp->getCartItem(), $cart_tmp->getCartPrice());
        if($cart_read!=NULL){
            foreach($cart_read as $key):               
    ?>
    <a class="btn btn-primary" target="_self" href="index.php?tmp_item=<?php echo base64_encode($key->tmp_product) ?>&tmp_price=<?php echo base64_encode($key->tmp_price) ?>">Add more Products</a>
    <?php 
    endforeach;
        }        
      }

And now I must rescue the String that returns the info that the user had saved

The String returns, for example:

1 ORANGE , A 23 GREEN , A 22 COBALT BLUE , ASDSADSAD 

All that string I have to transform it into a table and in each cell a text, so I was doing it, but I do not find it

    <tbody id="first_tabs">
                                 <?php if(isset($_GET["tmp_item"]) 
                                         && (!empty($_GET["tmp_item"])) 
                                         && ($_GET["tmp_item"]!="")
                                         && isset($_GET["tmp_price"])
                                         && (!empty($_GET["tmp_price"]))
                                         && ($_GET["tmp_price"]!="")){
                                        include_once 'admin/tmp_cart.php';
                                        $tmp_items = new temporalCarts();
                                        $tmp_items->setCartItem(base64_decode($_GET["tmp_item"]));
                                        $tmp_items->setCartPrice(base64_decode($_GET["tmp_price"]));
                                        $tmp_name = $tmp_items->getCartItem();
                                        $STR_REPLACE_1 = str_replace(",","<tr><td></td></tr>",$tmp_items->getCartItem());
                                        echo $tmp_items->getCartItem();
                                         }
                                                                    
                                     ?>
                                   
                                         <?php  ?>
                                 </tbody>
    
asked by Carlos Estarita 31.07.2017 в 23:18
source

1 answer

1

What I can think of that you could do is break the chain with explode and then what happens Put it on the table, as follows:

$STR_EXPLODE_1 = explode(",", $tmp_items->getCartItem());
<tr>
    <td><?php echo $STR_EXPLODE_1[0]; ?></td>
    <td><?php echo $STR_EXPLODE_1[1]; ?></td>
    <td><?php echo $STR_EXPLODE_1[2]; ?></td>
    <td><?php echo $STR_EXPLODE_1[3]; ?></td>
</tr>

Edition:

For when you do not know the indexes you can do a foreach cycle, in the following way:

foreach($STR_EXPLODE_1 as $EXPLODE_1) {
    $EXPLODE_1 = trim($EXPLODE_1);
    <td><?php echo $EXPLODE_1; ?></td>
}

I hope I can help you.

    
answered by 01.08.2017 / 00:28
source