0

I'm trying to create a website in my scenario I need to retrieve many rows of data from my database. Each row displayed should have a button next to it with a unique name. The only problem is getting the button to have a unique name.

<?php
        require_once 'php/login.php';
        $conn = new mysqli($hn, $un, $pw, $db);
        if ($conn->connect_error) die($conn->connect_error);

        $query = "SELECT * FROM Bets_info WHERE BetGroup = '1'";
        $result = $conn->query($query);
        if(!result) die($conn->error);

        $rows = $result->num_rows;

        for ($j = 0; $j < $rows ; ++$j)
        {
          $result->data_seek($j);
          $row = $result->fetch_array(MYSQLI_ASSOC);

          echo '<button id="myBtn">Bet</button>  ';
          echo ' '  .$row['BetWin'] . '/';
          echo ' '  .$row['BetLose']  . ' : ';
          echo ' '  .$row['BetDescription'] . '<br><br>'; 

        }
        $result->close();
        $conn->close();
      ?>

I have tried to do the following

for ($j = 0; $j < $rows ; ++$j)
        {
          $result->data_seek($j);
          $row = $result->fetch_array(MYSQLI_ASSOC);

          echo '<button id="myBtn<? $j ?>">Bet</button>  ';
          echo ' '  .$row['BetWin'] . '/';
          echo ' '  .$row['BetLose']  . ' : ';
          echo ' '  .$row['BetDescription'] . '<br><br>'; 

        }

This does not make the buttons called btn1, btn2, btn3 but instead it just calls all the buttons "myBtn<? $j ?>".

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Hack_Hut
  • 174
  • 3
  • 14

3 Answers3

3
for ($j = 0; $j < $rows ; ++$j)
    {
      $result->data_seek($j);
      $row = $result->fetch_array(MYSQLI_ASSOC);

      echo '<button id="myBtn'.$j.'">Bet</button>  ';
      echo ' '  .$row['BetWin'] . '/';
      echo ' '  .$row['BetLose']  . ' : ';
      echo ' '  .$row['BetDescription'] . '<br><br>'; 

    }

You can not put <? $j ?> inside echo

You want to put '.$j.' rather than it, And the code will work fine, :)

I hope its helps you :)

Mohammed Alhanafi
  • 886
  • 1
  • 9
  • 22
2

Concatenate the row number with the . operator, like this:

echo '<button id="myBtn' . $j . '">Bet</button>  ';
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
Chris H.
  • 41
  • 2
0

you should do like this :

for ($j = 0; $j < $rows ; ++$j)
    {
      $result->data_seek($j);
      $row = $result->fetch_array(MYSQLI_ASSOC);

      echo '<button id="myBtn'.$j.'">Bet</button>  ';
      echo ' '  .$row['BetWin'] . '/';
      echo ' '  .$row['BetLose']  . ' : ';
      echo ' '  .$row['BetDescription'] . '<br><br>'; 

    }
MEDALI
  • 55
  • 5