Create custom variables

Asked

Viewed 60 times

0

Well, I’ve got a flea here behind my ear and I’m having trouble solving the following problem:

  $select = $mysqli->query("SELECT * FROM payments WHERE client_email='$xemail'");
  $row = $select->num_rows;
  $linhas = 0;
  while($linhas < $row) {
  $get = $select->fetch_array();
  $top_1 = $get[0];
  $top_2 = $get[1];
  $top_3 = $get[2];
  $top_4 = $get[3];
  $top_5 = $get[4];
  $top_6 = $get[5];
  $top_7 = $get[6];
  $top_8 = $get[7];
  $top_9 = $get[8];
  $top_10 = $get[9];
  $top_11= $get[10];
  $top_12 = $get[11];
  $linhas++;
  }
  echo '<div id="transaction_id">'.$top_2.'</div>';
  echo '<div id="client_email">'.$top_3.'</div>';
  echo '<div id="payment_method">'.$top_4.'</div>';
  echo '<div id="payment_method_transaction">'.$top_5.'</div>';
  echo '<div id="transaction_status">'.$top_6.'</div>';
  echo '<div id="transaction_date">'.$top_7.'</div>';
  echo '<div id="transaction_date_last">'.$top_8.'</div>';
  echo '<div id="product">'.$top_9.'</div>';
  echo '<div id="product_value">'.$top_10.'</div>';
  echo '<div id="client_name">'.$top_11.'</div>';
  echo '<div id="status">'.$top_12.'</div>';

Basically this will take all the purchases made for that customer’s email. But the problem is that the variables could not be called $top_1, they should receive an additional number each time the loop repeated, after top_12 and went back to top_1, instead of top_1 was top_13 and so on... and give an echo in the variable to be able to catch it in CSS... I don’t even know if this title would be ideal, but I searched for it in Google and I didn’t find anything, I honestly didn’t even know how to research this problem... :(

What I want to do is take all the shopping data and display it on a page to the customer. The system will read about 5 records and then it will display page 2, and so on... I don’t even know if this script is the best way to do it rs... but it was the one I tested and it worked. Can you create these variables with different names as the loop repeats? Is there a better way to do this?

2 answers

2

The right one would be to use a multidimensional array. And it’s not a good thing to use mysqlli either see here

I think it’s a nice solution like this:

First replace mysqlli with PDO:

<?php
$ret = array();
$contador = 0;
try{
$db = new PDO("dbtype:host=yourhost;dbname=yourdbname;charset=utf8","username","password");
$query=$db->prepare("SELECT * FROM payments WHERE client_email='?'");
$query->excute(array($xemail));
while($row=$query->fetch(PDO::FETCH_OBJ)) {
   $ret[$contador]['transaction_id'] = $row->nomeDaColuna;
   $ret[$contador]['client_email'] = $row->nomeDaColuna;
   $ret[$contador]['payment_method'] = $row->nomeDaColuna;
   $ret[$contador]['payment_method_transaction'] = $row->nomeDaColuna;
   $ret[$contador]['transaction_status'] = $row->nomeDaColuna;
   $ret[$contador]['transaction_date'] = $row->nomeDaColuna;
   $ret[$contador]['transaction_date_last'] = $row->nomeDaColuna;
   $ret[$contador]['product'] = $row->nomeDaColuna;
   $ret[$contador]['product_value'] = $row->nomeDaColuna;
   $ret[$contador]['client_name'] = $row->nomeDaColuna;
   $ret[$contador]['status'] = $row->nomeDaColuna;
   $contador++;
}
}catch(PDOException  $e ){
echo "Error: ".$e;
}
?>

Ready now your object has been populated securely. To render would be something like this:

<?php
   foreach($ret as $value){
      foreach($value as $key=>$v){
         echo "<div id='$key'>$v</div>";
      }
   }
?>
  • 2

    Brother, I think you have confused, functions mysqli_ with mysql_ . As far as I know there are not yet, contrasts regarding the functions mysqli_.

  • 1

    And, I was going to say the same, I think he got confused... mysql and mysqli are different, mysql has been deprecated and no more updates, mysqli remains normal... so much so that in his reference explains exactly this, mysql has been deprecated, and now the correct use would be mysqli

  • But I like this foreach, I don’t know much about programming... so I’ll take a look at it soon. While I wait to see if there are any more answers.

  • 1

    You can use the aproach you think best with mysql. My solution is this =]

1


I believe that solves your problem:

<?php
    $count = 0;
    $res = $mysqli->query("SELECT * FROM payments WHERE client_email='$xemail'");

    while($row = $res->fetch_array()){
      $top[$count] = $row['nomeDaColuna'];
      $count++;
    }

    echo '<div id="transaction_id">'.$top[0].'</div>';
    echo '<div id="client_email">'.$top[1].'</div>';
    echo '<div id="payment_method">'.$top[2].'</div>';
    echo '<div id="payment_method_transaction">'.$top[3].'</div>';
    echo '<div id="transaction_status">'.$top[4].'</div>';
    echo '<div id="transaction_date">'.$top[5].'</div>';
    echo '<div id="transaction_date_last">'.$top[6].'</div>';
    echo '<div id="product">'.$top[7].'</div>';
    echo '<div id="product_value">'.$top[8].'</div>';
    echo '<div id="client_name">'.$top[9].'</div>';
    echo '<div id="status">'.$top[10].'</div>';
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.