Home > Enterprise >  How to create unique ID's for each button in a loop using php echo
How to create unique ID's for each button in a loop using php echo

Time:07-05

I am Having a problem with the button Id id="option_0_0 ,id="option_0_1 and id="option_0_2

Since Now i'm Using echo php, i'm limited to the id's i can give each button to make each button unique,and the loop keeps repeating the id name on each button, how do create unique ID's for each button?


<div >

  <div >Bet Slip</div>
  
  <div id="box" ></div>

  <div>Net Win N$</div>

  <b type="number">300</b>

  <output name="NetWin" for="a b"></output>

  <input type="number" max="100" width="10px">

  <button >Bet</button><br />

</div>



<br>

<table id="Table1" >
    
    <thead>

        <tr>
            
            <th>League</th>
            <th>Home</th>
            <th>Draw</th>
            <th>Away</th>
            <th>Kickoff</th>

        </tr>

    </thead>



    <tbody >

        <tr><th>AFF Women's Championship Group Stage<td id="label" >1<td id="label" >x<td id="label" >2</th></tr>
<tr>
            
                <td >Philippines - Australia</td> 
                <td><input type="button"  id="option_0_0" value ="20.00" /></td>
                <td><input type="button"  id="option_0_1" value ="8.75" /></td>
                <td><input type="button"  id="option_0_2" value ="1.04" /></td>
                <td >7/4/2022 13:00</td> 
                
            </tr><tr><th>Africa Women Cup of Nations<td id="label" >1<td id="label" >x<td id="label" >2</th></tr>
<tr>
            
                <td >Nigeria - South Africa</td> 
                <td><input type="button"  id="option_0_0" value ="2.00" /></td>
                <td><input type="button"  id="option_0_1" value ="3.05" /></td>
                <td><input type="button"  id="option_0_2" value ="3.45" /></td>
                <td >7/4/2022 19:00</td> 
                
            </tr><tr>
            
                <td >Burundi - Botswana</td> 
                <td><input type="button"  id="option_0_0" value ="3.65" /></td>
                <td><input type="button"  id="option_0_1" value ="3.10" /></td>
                <td><input type="button"  id="option_0_2" value ="1.95" /></td>
                <td >7/4/2022 22:00</td> 
                
            </tr><tr><th>Argentina Liga Profesional Argentina<td id="label" >1<td id="label" >x<td id="label" >2</th></tr>
<tr>


this loop is generated from php echo like this

            echo  

            '<tr>
            
                <td >'. $row["COL 2"] .'</td> 
                <td><input type="button"  id="option_0_0" value ="'. $row["COL 3"] .'" /></td>
                <td><input type="button"  id="option_0_1" value ="'. $row["COL 4"] .'" /></td>
                <td><input type="button"  id="option_0_2" value ="'. $row["COL 5"] .'" /></td>
                <td >'. $row["COL 6"] .'</td> 
                
            </tr>';
        
        }
        
    
        ?>

CodePudding user response:

I assume you're asking how to change the id with jquery ? If so then this should do that for you

$(".decimals").each(function(index) {
  $(this).attr("id", index);
});

I hope this helps

CodePudding user response:

You are echoing from your PHP code, therefore I assume that you need the solution in PHP code. If so, here you go:

$i = 0;

foreach ($rows as $row) {

echo
    '<tr>'.
    '<td >'. $row["COL 2"] .'</td>'.
    '<td><input type="button"  id="option' . $i   . '" value ="'. $row["COL 3"] .'" /></td>'.
    '<td><input type="button"  id="option' . $i   . '" value ="'. $row["COL 4"] .'" /></td>'.
    '<td><input type="button"  id="option' . $i   . '" value ="'. $row["COL 5"] .'" /></td>'.
    '<td >'. $row["COL 6"] .'</td>'.
    '</tr>';
}
  • Related