0

I want to fetch some data from database and show it in the form of HTML table.

I want the first column of the table to show the number of row like 1,2,3,4...

This is my foreach loop

$i=0;    
foreach($sql as $sql)    
{
    echo "<tr>";
        echo "<td>$i++</td>";
        echo "<td>$sql->jobseeker_name</td>";
        echo "<td>$sql->jobseeker_phone</td>";
        echo "<td>$sql->login_email</td>";
    echo "</tr>";
}

It is not working and I don't know what is wrong with it.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Ajmal Razeel
  • 1,663
  • 7
  • 27
  • 51
  • Side note: That produces code in a single line. By concatenating `. "\n"` inside the echo'd (table) tags, it would produce clean HTML. This is essentially a good thing when it comes to debugging while viewing HTML source. – Funk Forty Niner Jul 26 '17 at 00:45
  • Possible duplicate of [PHP Incrementing?](https://stackoverflow.com/questions/17386454/php-incrementing) – mickmackusa Jul 26 '17 at 00:59
  • Better practice would be to not use `$sql` to represent each "row". This will become an issue if you need to re-use the `$sql` array -- when the loop finishes, what used to be an array of subarrays containing rows, will then be the subarray containing the last row. – mickmackusa Jul 26 '17 at 01:08
  • Here is a demo that exposes your initial issue and some potential hiccups when incrementing a counter: [Demo](http://sandbox.onlinephpfunctions.com/code/95ebef95025d1e3e869f4a1a2c3a6dda2485e2d0) – mickmackusa Jul 26 '17 at 01:10

5 Answers5

3

Don't write $i++where you want the echo. Echo it first and at the end of the loop, add $i++ to incement the value:

<?php    

$i = 1;    

foreach ($sql as $sql) {
    echo "<tr>";
    echo "<td>$i</td>";
    echo "<td>$sql->jobseeker_name</td>";
    echo "<td>$sql->jobseeker_phone</td>";
    echo "<td>$sql->login_email</td>";
    echo "</tr>";

    $i++;
}
localheinz
  • 9,179
  • 2
  • 33
  • 44
Johannes
  • 64,305
  • 18
  • 73
  • 130
3

The basic version of string interpolation is ignoring the ++ operator, as it only knows about simple variables - see String Parsing

If you want a nicer-looking bit of code, you can do something like this:

<!-- language: lang-php -->
<?php    

$i = 1;    
foreach($sql as $sql): ?>

   <tr>
     <td><?= $i++ ?></td>
     <td><?= $sql->jobseeker_name ?></td>
     <td><?= $sql->jobseeker_phone ?></td>
     <td><?= $sql->login_email ?></td>
   </tr>
<?php endforeach;
Sam Dufel
  • 17,560
  • 3
  • 48
  • 51
  • Ah, didn't see that he wanted to start with 1, I merely fixed the increment in his code. Regarding the formatting, if you're dealing with a non-trivial bit of html, you can get syntax highlighting and the like on your html if you leave the php context. I find it much easier to work with, especially when it comes to matching up tags, noticing unterminated html attributes, etc. – Sam Dufel Jul 26 '17 at 16:42
1

Normally when I vote to close a question as duplicate, I will not post an answer, but there are several opportunities to educate on this question that are difficult to delineate in mere comments.

It is vital, if you are going to increment in the echo line, that the ++ comes before $i. If the ++ comes after $i, then the initial value of $i is echoed THEN the incrementation occurs.

Assuming I've got the structure of your resultset right, this is how I would write the snippet:

Code: (Demo)

$sql=[
    (object)['jobseeker_name'=>'A','jobseeker_phone'=>'B','login_email'=>'C'],
    (object)['jobseeker_name'=>'D','jobseeker_phone'=>'E','login_email'=>'F'],
    (object)['jobseeker_name'=>'G','jobseeker_phone'=>'H','login_email'=>'I']
];
foreach($sql as $i=>$row){
    echo "<tr>";
        echo "<td>",++$i,"</td>";
        echo "<td>$row->jobseeker_name</td>";
        echo "<td>$row->jobseeker_phone</td>";
        echo "<td>$row->login_email</td>";
    echo "</tr>";
}

Output:

<tr>
    <td>1</td>
    <td>A</td>
    <td>B</td>
    <td>C</td>
</tr>
<tr>
    <td>2</td>
    <td>D</td>
    <td>E</td>
    <td>F</td>
</tr>
<tr>
    <td>3</td>
    <td>G</td>
    <td>H</td>
    <td>I</td>
</tr>

Additionally, as a matter of best practice:

  • Use a new variable name to distinguish each row of the $sql array of objects. If you use foreach($sql as $sql){ then if you need to access $sql again, you will only be able to access the last iterated subarray of $sql This is because the foreach loop is constantly overwriting the input array.

  • Use commas rather than dots to concatenate with echo. It is a nano-optimization, but there is no downside to doing this a matter of habit.

  • Also as a matter of nano-optimization, it is faster to use "pre-incrementation" (++$i) rather than "post-incrementation" ($i++) in all cases where the outcome is the same. What's the difference between ++$i and $i++ in PHP?

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
0

You just need to escape your i++

echo "<td>".$i++."</td>";

Because $i++ is a PHP incrementation it needs to be parsed.

So the complete code would look like this:

<?php    
$i=1;    
        foreach($sql as $sql)    
        {
         echo "<tr>";
         echo "<td>".$i++."</td>";
         echo "<td>$sql->jobseeker_name</td>";
         echo "<td>$sql->jobseeker_phone</td>";
         echo "<td>$sql->login_email</td>";
         echo "</tr>";
        }
?>

Tested and working as expected. Proof of concept here: https://3v4l.org/mUqZ1.

Patrick Simard
  • 2,294
  • 3
  • 24
  • 38
-2

try this code

   $i=0;    
   foreach($sql as $sql){
      echo "<tr>";
      echo "<td>$i++</td>";
      echo "<td>$sql->jobseeker_name</td>";
      echo "<td>$sql->jobseeker_phone</td>";
      echo "<td>$sql->login_email</td>";
      echo "</tr>";
      i++;
   }
  • 1
    Not only is this snippet clearly broken/wrong/untested, it is entirely unexplained. Even if this answer was correct, the post adds no new/unique value to the page and therefore will only waste researchers' time. This is an excellent representation of a post that should be removed -- either by the OP or by the community -- in the name of curating good content on Stack Overflow. – mickmackusa Aug 12 '20 at 13:10