Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Louis Darby
Louis Darby
5,149 Points

Making a shortlist with PHP and MySQL

Hi, I am trying to make a shortlist based on results from a sql query on a different page. This is what I have so far:

I am running a MySQL query, expressing the results as a FETCH_OBJ and then assigning each property a variable so they can be printed out later down the page.

<?php $results = $db->query("SELECT * FROM venues"); $venues = $results->fetchAll(PDO::FETCH_OBJ);

foreach ($venues as $venue) { $i = 0; $name = ($venues[$i]->name); $i++; };

I am then looping through the results and for each creating a button where you can 'Add to Shortlist'.

<form action="cart.php" method="post"> <input type="button" name="<?php echo $name?>" value="Add <?php echo $name ?> to Shortlist"/> </form>

I'd then like to retrieve this name in a new file 'cart.php'

<p>The name of the selected place is <?php echo $_POST['name']?></p>

and below this show the relevant information of this place by using the name to generate another query from the database.

However in my cart.php file I cannot seem to get the name showing.

Is this possible or have I done it completely wrong? Would greatly appreciate any help/comments.

1 Answer

In foreach cycle, everything between brackets is run for every item in the array. Therefore, $i will always be zero and $name will end up with name of the first venue in the table.

Did you put name="name" on your button? Can you give us your code as a whole?