0

I have created a table with 3 columns and rows with the size of an array pulled from the database. I have got the value but I want to create a button inside the cells with the name from pname and when you click that button it will take you to the value of plink. I tried using <input type="button" value=" " onclick="window.open( )", but it doesn't seem to pass the arrays. I am sure there is an easier/ better way to do it with JavaScript, but I am not familiar with JavaScript. Please help me out. Here is my code.

I want the format 3 columns and infinite rows(based on the data).

    <!DOCTYPE html>

    $pname = array();
    $plink = array();
    $results = $wpdb->get_results("SELECT name, link FROM `wptable`);
    if(!empty($results)) {
       foreach($results as $row){
        $pname[] = $row->name;
        $plink[] = $row->link;
       }
    }

    $num = 0;
    echo "<table class='table'>";
    echo "<tbody>";
    echo "<br><br>";

    $quant_row = count($pname)/3;
    $quant_col = 3;

    for ($count_row = 0; $count_row < $quant_row; $count_row++) 
    {
        echo "<tr>";

        for ($count_col = 0; $count_col < $quant_col; $count_col++) 
        {

                echo "<td>";
                echo $plink[$num];
                echo "</td>";
                $num++;

       }
        echo "</tr>";
    }
    echo "</tbody>";
    echo "</table>";

    ?>
Patrick Q
  • 6,373
  • 2
  • 25
  • 34

2 Answers2

0

You just need to echo a button inside an anchor:

echo '<a href="' . $plink[$num] . '"><button type="button">' . $pname[$num] . '</button></a>';
Arleigh Hix
  • 9,990
  • 1
  • 14
  • 31
0

try this.

echo "<td>";
echo "<button onclick=\"Document.getElementById('link$num').display=state$num? 'none' :'block';state$num=!state$num; \">". $pname[$num] ."</button><div id='link$num'>". $plink[$num]."<div>";

echo "";

when you click on $pname , you show the $plink. reclick and hide the content

pagliaccio
  • 65
  • 1
  • 5