Jump to content

how to change a php variable by clicking on a link


xtoc

Recommended Posts

This code will load and show all tables automatically and display the selected table (depends on what nr tbnr is)
What i want to do is , by clicking on a link it would change the variable tbnr.

So, i've 2 tables named , "Customers" : tbnr = 0  / "Books" : tbnr = 1
When i click on a link named Customers , it has to change the variable tbnr to 0.
When i click on a link named Books , it has to change the variable tbnr to 1.


[color=blue]My code :[/color]

[code]
<?php
$i=0;
$j=0;
$tbnr=1;
mysql_connect("localhost", "1234", "1234") or die(mysql_error());
mysql_select_db("bedrijf") or die(mysql_error());
// Get all the data from the "example" table
$tables = mysql_query("SHOW TABLES")
or die(mysql_error()); 

echo "<table border='1'>";
echo "<tr> <th>Table</th> </tr>";
// keeps getting the next row until there are no more to get

while($table= mysql_fetch_array( $tables )) {
// Print out the contents of each row into a table
    $my_array[$i] = $table[0];

echo "<tr><td>";
    echo $my_array[$i];
echo "</td></tr>";
    $i +=1;
}

echo "</table>";

//------------- fill collnames
$cols = mysql_query("SHOW COLUMNS FROM " . $my_array[$tbnr])
or die(mysql_error()); 

echo "<table border='1'>";
echo "<tr>";

while($col= mysql_fetch_array( $cols )) {
$my_array2[$j] = $col[0];
echo "<th>";
echo $my_array2[$j];
echo "</th>";
$j +=1;
}
$j = 0;

//------------- fill table

$result = mysql_query("SELECT * FROM " . $my_array[$tbnr]) // vul result met alle data van tabel
or die(mysql_error());
   

    while($row = mysql_fetch_array( $result )) { // gaat de eerste rij opvragen
    echo "<tr>";
for ($k = 0; $k < count($my_array2); $k++){
    echo "<td>";
echo $row[$my_array2[$k]]; // vraagt als eerste id op want $j = 0
echo "</td>";
    };
    echo "</tr>";
    };




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



?>
[/code]
You could place your variable as part of the link and use $_GET to retrieve the data when the form is submitted. However other people prefer to use $_POST method for security reasons and to do this you would need to use a hidden input on your form and then change its value by using something like Javascript before you form is submitted.

It really depends on how you want to go about it.

The get method link would be,

[code=php:0]
<a href='www.mywebsite.com/myscript.php?myvariable=1'>my link</a>
[/code]

and then you retrieve the information using,

$_GET['myvariable'];
You need to make you customers link look like.

[code]
<a href="page.php?tbnr=0">Customers</a>
[/code]
and books....
[code]
<a href="page.php?tbnr=1">Books</a>
[/code]

Then....

[code=php:0]
$result = mysql_query("SELECT * FROM " . $my_array[$_GET['tbnr']])
[/code]

You'll also need to validate this number before trying to use it in your query but you get the idea.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.