简体   繁体   中英

PHP - Why is it concatenating at the beginning?

I am trying to concatenate a string returned from a function with another string, but the problem is that instead of concatenating at the end of the string, it is inserting the value returned by the function at the beginning of the string.

Here's the piece of the code that does that:

$dados = '';
if ($unidade == 'Goiânia') {
    $dados = "58, 63, 66, 66, 81, 85, 86, 86, 89, 90, 90,";
} elseif ($unidade == 'Jataí') {
    $dados = "11, 15, 18, 20, 21, 23, 23, 24, 24, 25, 25,";
} elseif ($unidade == 'Catalão') {
    $dados = "9, 14, 16, 19, 24, 25, 25, 25, 25, 26, 26,";
} elseif ($unidade == 'Goiás') {
    $dados = "1, 1, 1, 1, 3, 3, 3, 3, 5, 6, 7";
}
// Concatenating the string with the function
// the function returns a string.
$dados .= consultaSimplesRetornaString($sql);
echo $dados;

My expected output is (in the case of the first IF being true):

58, 63, 66, 66, 81, 85, 86, 86, 89, 90, 90,

What I'm actually getting is:

, 58, 63, 66, 66, 81, 85, 86, 86, 89, 90, 90,

I'm guessing that this kind of concatenation does not work with functions, since it works just fine with php variables, and I should do it another way.

Thanks!

EDIT:

As requested, here's the function consultaSimplesRetornaString() and the $sql parameter: (The function is working fine, I used it through the entire document).

function consultaSimplesRetornaString($sql)
{
  // Connecting to te db
  $conn = connect();
  $result = $conn->query($sql);
  if (!$result)
    echo $conn->error;

  $aux = 0;
  while ($row = $result->fetch_assoc()) {
    $array[$aux] = $row["count"];
    $aux++;
  }

  $str = "";
  foreach($array as $key => $value) {
    $value = round($value, 2);
    $str.= "$value ,";
  }

  $conn->close();
  echo ($str);
}

And here's the $sql parameter:

$anoSelecionadoPOST and $unidade are just simple variables, and are working just fine.

$sql = "SELECT COUNT(distinct curso) AS count FROM `$anoSelecionadoPOST` WHERE `ano_ingresso`= '$anoSelecionadoPOST' and `Regional` = '$unidade'";

I believe your database holds broken data as the code should rather work. This line in particular smells:

$array[$aux] = $row["count"]

and as it populates your $array with whatever comes from the query results. You may want to add var_dump($row['count']); to that loop to verify what each added value is and either fix your database content or, if that's valid case to have no 'count' numeric do some checks to prevent using empty data (or use 0 instead).

Also, gluing content of $array in a loop is a waste. You should use implode() for that.

PS: In general consultaSimplesRetornaString() is pretty bad code, in terms of error handling.

Because of echo ($str); in the consultaSimplesRetornaString function. So you first echo result of function, then echo $dados;

Try this:

$dados = [];
if ($unidade == 'Goiânia') {
    $dados = [58, 63, 66, 66, 81, 85, 86, 86, 89, 90, 90];
} elseif ($unidade == 'Jataí') {
    $dados = [11, 15, 18, 20, 21, 23, 23, 24, 24, 25, 25];
} elseif ($unidade == 'Catalão') {
    $dados = [9, 14, 16, 19, 24, 25, 25, 25, 25, 26, 26];
} elseif ($unidade == 'Goiás') {
    $dados = [1, 1, 1, 1, 3, 3, 3, 3, 5, 6, 7];
}
// Fill the array with the function results from DB
// the function returns an array.
$dados = consultaSimplesRetorna($sql, $dados);
echo join(',', $dados);

function consultaSimplesRetorna($sql, $result = null)
{
  // Connecting to te db
  $conn = connect();
  $rowset = $conn->query($sql);
  if (!$rowset) echo $conn->error;
  if (!$result) $result = [];
  while ($row = $rowset->fetch_assoc()) {
    $result[] = $row["count"];
  }
  $conn->close();
  return $result;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM