How to display id as form counter

Asked

Viewed 129 times

-2

I’m creating a form counter and I’d like to know if I’m going the right way ?

This is my select query

<?php
error_reporting(-1);
ini_set('display_errors', 'On');

//Criar a conexao
$link = new mysqli ("localhost", "root", "", "peixaria");
if($link->connect_errno){
     echo"Nossas falhas local experiência ..";
     exit();
}



         $sql= "SELECT MAX(m.id_pedido)
                FROM mpedido as m WHERE id_pedido
                ORDER BY m.id_pedido DESC
                LIMIT 1";
        $resultado_id= mysqli_query($link,$sql);

?>

and this is the php code that displays the id as counter . but type is giving an error

Notice: Array to string Conversion in C: xampp htdocs Requested projects.php on line 61

  <span>Comanda:</span>
                    <?php


                while($row = mysqli_fetch_array($resultado_id)){
                echo '<span>'.["id_pedido"].'</span>';

                }

                ?>

How could I make a counter from the id

  • It seems to me a duplicate of: http://answall.com/questions/178905/como-crea-um-campo-contador-em-php-a-partir-do-id-da-table

  • 'Cause no one could answer

  • I feel that all your questions end up being poorly answered or else not even answered are... I suggest a read on the topic [Ask] and make a [tour]

  • In fact as far as I can see, all your questions could fit the signage of "not clear enough"

  • I know what it’s like .

  • echo '<span>'.["id_pedido"].'</span>'; you’re making the id_pedido in an array when placed between brackets. Use $row['id_pedido'] to display the ID.

  • Array to string Conversion means that you are having an array printed as if it were a string. When there is such an error, change the echo for print_r to 'debug' the error line.

Show 2 more comments

2 answers

3


The mistake is in:

echo '<span>'.["id_pedido"].'</span>';

There is no such thing as.

When you use the code:

while($row = mysqli_fetch_array($resultado_id)){

}

Are you saying that as long as there are lines, you will get the array $row, or should be used:

while($row = mysqli_fetch_array($resultado_id)){

 echo '<span>'.$row["id_pedido"].'</span>';

}

But... that won’t work either. The reason is that there is no index id_pedido and the reason it doesn’t exist is because there’s no such thing defined in SELECT.


You don’t need to use the while() if there is the LIMIT 1, if the limit is 1 it will run only once.


Possible corrections:

Method 0. Use numerical index:

Part of While:

$row = mysqli_fetch_array($resultado_id);
echo '<span>'.$row[0].'</span>';

Method 1. Name the oxen:

Part of the Query:

$sql= "SELECT MAX(m.id_pedido) as ultimo FROM mpedido as m WHERE id_pedido ORDER BY m.id_pedido DESC LIMIT 1";

$resultado_id= mysqli_query($link,$sql);

Soon, now the SELECT has a result with the name ultimo for the result of MAX(m.id_pedido), which is easier.

Part of While:

$row = mysqli_fetch_array($resultado_id);
echo '<span>'.$row['ultimo'].'</span>';
  • it will display the right id ,? but like every time I open a new form, it will display the previous form id and how do I give a +1 increment to show the form id being filled ?

  • Use $row['ultimo'] + 1, will add 1 to the number. But, there is a logic problem there, if two people open the page they will see the same number as next, and if they both send only one of it will have such number.

  • Thank you very much .

  • Hello @Inkeliz , well used this code it is showing the previous id when clicking on the new form as I do to add a value so it shows the id that will be inserted in the form .

2

I’m sure I understand, but I believe you want something like:

//Criar a conexao
$link = new mysqli ("localhost", "root", "", "peixaria");
if($link->connect_errno){
 echo"Nossas falhas local experiência ..";
 exit();
}   

$sql= "SELECT MAX(m.id_pedido) as ultimo_id FROM mpedido as m";
$resultado_id= mysqli_query($link,$sql);
?>

As you used MAX(), it will return the highest value of the requested id_field, so if this is a sequential field it will already return the desired value.

Already in php you should do so

<span>Comanda:</span>
<?php
  while($row = mysqli_fetch_array($resultado_id)){
   echo '<span>'.$row["ultimo_id"].'</span>';
  }

?>

When you do this while, you receive a line with the returned information, but it comes as an array, so you need to access the index you created in SQL, in the case "ultimo_id".

I hope that’s what you’re looking for.

  • i want it to show the last id and add one more to it so the screen will show the id of the open form and it will be inserted .

  • You can do so in sql: $sql= "SELECT MAX(m.id_request)+1 as last";

  • Thank you very much bro

Browser other questions tagged

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