How do I concatenate two variables to form a sequential variable

Asked

Viewed 55 times

0

<?php
error_reporting(E_ERROR);
session_start();
$var_quant = $_SESSION['quantvend'];
$var_varejo_atacado = $_GET['varejo-atacado'];

$con = mysql_connect("localhost","root","");
mysql_select_db("edutricot");


for($i = 1; $i<=$var_quant ; $i++){
    $var_nome{$i} = $_GET['txtcod'.$i];
    $var_quantvend{$i} = $_GET['txtquant'.$i];
    $var_varejo_atacado = $_GET['varejo-atacado'];
    echo $var_varejo_atacado;
    echo $var_nome{$i};
    echo $var_quantvend{$i};
    echo "<br>";

    $query = "SELECT $var_varejo_atacado FROM estoque where nome = $var_nome.$i";
    $dados = mysql_query($query,$con);
    while($row=mysql_fetch_array($dados,MYSQL_ASSOC)){
        $var_valor{$i} = $row["$var_varejo_atacado"];
        $_GLOBALS['valor'.$i] = $var_valor{$i};

    }


    $_GLOBALS['prodquant'.$i] = $var_quantvend{$i};
    $var_valor{$i} = $_GLOBALS['valor'.$i];
}
?>

I’m new to dealing with php and needed someone to explain to me how I can concatenate two variables to put in Mysql query.

$query = "SELECT $var_varejo_atacado FROM estoque where nome = $var_nome.$i";

I am trying to concatenate using the . but always from the error, would have some way to make this concatenation?

$var_nome{$i} = $_GET['txtcod'.$i];

This is the line where I’m trying to get the information, but I realized that the $var_nome{$i} not concatenating the way I expected, not creating for example $var_nome1, $var_nome2 etc..

  • I think you should review the code and use array instead of continuing with the line of reasoning of using sequential variables, both in HTML and PHP.

  • Use array.

1 answer

0

First thing, better review the code with sequential variables, there are better ways to work your application. But as we are already here. Maybe it is better to use array and make a "Where in" in the query

$array = [];    
for() {
       $array[] = $var_nome.$i       
    }

after vcs uses query fetch outside for, has mto processing ai

$query = "SELECT * FROM $var_varejo_atacado WHERE nome IN (".implode(',',$array).")"; 
$dados = mysql_query($query,$con);
    while($row=mysql_fetch_array($dados,MYSQL_ASSOC)){
        $var_valor{$i} = $row["$var_varejo_atacado"];
        $_GLOBALS['valor'.$i] = $var_valor{$i};

    }

Browser other questions tagged

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