Skip to content
Advertisement

php post value from FOREACH to another page

I’ve never asked any question here before, but after spending many hours searching for a hint I decided to ask a question.

I have a project at school where I need to create a table from DB for all records for one user, and then create an expanded view for all details for a chosen record.

So far I have:

$user = 'myuser';
$pass = 'mypassword';
$db = new PDO( 'mysql:host=localhost;dbname=mydb', $user, $pass );
$sql = "
SELECT  id,login                                                                                                        
FROM quotes_taxi                                                                                            
WHERE login='[usr_login]'";
$query = $db->prepare( $sql );                                                                           
$query->execute();                                                                           
$results 

where ‘id’ is obviously ID for each record. Then I create a table using FOREACH

<?php foreach( $results as $row ){
echo '<tr>';
echo '<td>'; echo $row['id']; echo '</td>';
echo '<td>'; echo $row['login']; echo '</td>';
echo '<td>'; echo '<a href="../view/index.php?id=$row['id']"> View All </a>'; 
echo '</td>';
echo "</tr>";
}
?>  

the problem I am having is: how can I attached that ‘id’ within the link (and it needs to be a separate button for each ID), so I can pass it to the view/index.php where I am using

$quote_id = $_GET["id"];

to get all other details of a chosen ID…

I know I have an error in that line, I just cannot figure it what. I also assume it is an easy problem, but I just cannot get my head around it

any help is most appreciated. Thank you

Advertisement

Answer

Try to separate your PHP and HTML that way it will be easier to find errors quickly. Try the following code.

<table>
    <tr>
        <th>Id</th>
        <th>Login</th>
        <th>View</th>
    </tr>
    <?php foreach ($results as $row) : ?>
        <tr>
            <td><?php echo $row['id']; ?></td>
            <td><?php echo $row['login']; ?></td>
            <td><a href="../view/index.php?id=<?php echo $row['id']; ?>"> View All </a></td>
        </tr>
    <?php endforeach; ?>
</table>

Output

foresch output

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement