By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,347 Members | 1,390 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,347 IT Pros & Developers. It's quick & easy.

PHP, dropdown and aquiring the information

P: 2
Hello guys, this is my first post here. I tried searching for the answer, but I found out pretty quickly that I wouldnt find the perfect answer. I am pretty new to programming, but Im taking a class in c-programming and I am also trying to learn php and mysql on the side.

What I have done successfully is:
Get a server with mysql and php.
Getting mysql working, adding the database and the table.
Retrieving the information from the table and inserting it into a dropdownmenu(code will follow)

What i want:
When an item in the dropdownmenu(dpm) is selected, the user gets forwarded to a new page, where the result is displayed. If the user wishes to make more choices from the dpm , he can click on a button forwarding him back to the dpm. When he returns to the dpm the choices he has made previously has disappeared. When he makes a new choice, the new choice is listed on the resultpage with his previous choices, sorted by date.

My thoughts:
I have been thinking of a solution, and fact of the mather is I copied a working code from a guide, because I am pretty fresh to php. I think that the results from his choices in the dpm needs to be added to a new table in the database, and if i could get that to work, there wouldnt be any problems. :)

The code:

<?php
<form SRC="result.php">
/* this is something i have been trying, and it didnt work. It would solve my problem with retrieving the choice in the menu, but I still wouldnt know how to make the users previous choices disappear when he returns. */

print ("Please select your favorite clowns:");?><br><br><?
mysql_connect('xXxXx','xXxXxXx','veryxXxXxXx');
mysql_select_db('clowndatabase');


$sql = 'SELECT * FROM clowntable';
$res = mysql_query($sql) or die(mysql_error());
while ($rec = mysql_fetch_assoc($res)) $clowns[] = $rec;
//die('<pre>'.print_r($clowns,true));


echo '<SELECT name="dropdown">';
foreach ($clowns as $c)
{
if ($c['id'] == $_GET['id'])
echo "<OPTION value=\"{$c['id']}\" SELECTED>{$c['clowns']}</OPTION>\n";
else
echo "<OPTION value=\"{$c['id']}\">{$c['clowns']}</OPTION>\n";
}
echo '</SELECT>';
// </form>
?>


I hope I have made my post clean. And that my problem is easily understandable. Also, if anyone can tell me how to make a linechange in php, that would be great. Up to this point, I jump out of the php-tags and make a <br>. Its pretty ugly and not very good coding.
Feb 6 '08 #1
Share this Question
Share on Google+
2 Replies


rpnew
100+
P: 188
Hello guys, this is my first post here. I tried searching for the answer, but I found out pretty quickly that I wouldnt find the perfect answer. I am pretty new to programming, but Im taking a class in c-programming and I am also trying to learn php and mysql on the side.

What I have done successfully is:
Get a server with mysql and php.
Getting mysql working, adding the database and the table.
Retrieving the information from the table and inserting it into a dropdownmenu(code will follow)

What i want:
When an item in the dropdownmenu(dpm) is selected, the user gets forwarded to a new page, where the result is displayed. If the user wishes to make more choices from the dpm , he can click on a button forwarding him back to the dpm. When he returns to the dpm the choices he has made previously has disappeared. When he makes a new choice, the new choice is listed on the resultpage with his previous choices, sorted by date.

My thoughts:
I have been thinking of a solution, and fact of the mather is I copied a working code from a guide, because I am pretty fresh to php. I think that the results from his choices in the dpm needs to be added to a new table in the database, and if i could get that to work, there wouldnt be any problems. :)

The code:

<?php
<form SRC="result.php">
/* this is something i have been trying, and it didnt work. It would solve my problem with retrieving the choice in the menu, but I still wouldnt know how to make the users previous choices disappear when he returns. */

print ("Please select your favorite clowns:");?><br><br><?
mysql_connect('xXxXx','xXxXxXx','veryxXxXxXx');
mysql_select_db('clowndatabase');


$sql = 'SELECT * FROM clowntable';
$res = mysql_query($sql) or die(mysql_error());
while ($rec = mysql_fetch_assoc($res)) $clowns[] = $rec;
//die('<pre>'.print_r($clowns,true));


echo '<SELECT name="dropdown">';
foreach ($clowns as $c)
{
if ($c['id'] == $_GET['id'])
echo "<OPTION value=\"{$c['id']}\" SELECTED>{$c['clowns']}</OPTION>\n";
else
echo "<OPTION value=\"{$c['id']}\">{$c['clowns']}</OPTION>\n";
}
echo '</SELECT>';
// </form>
?>


I hope I have made my post clean. And that my problem is easily understandable. Also, if anyone can tell me how to make a linechange in php, that would be great. Up to this point, I jump out of the php-tags and make a <br>. Its pretty ugly and not very good coding.

Hi,
If you want that when user returns to the previous page his/her last choice should be disappear then change the following code snap..
[PHP]
foreach ($clowns as $c)
{
if ($c['id'] == $_GET['id'])
echo "<OPTION value=\"{$c['id']}\" SELECTED>{$c['clowns']}</OPTION>\n";
else
echo "<OPTION value=\"{$c['id']}\">{$c['clowns']}</OPTION>\n";
}
[/PHP]
to below given
[PHP]
foreach ($clowns as $c)
{
echo "<OPTION value=\"{$c['id']}\">{$c['clowns']}</OPTION>\n";
}
[/PHP]
And may also have to make a little bit change on the page where you are showing users choices... so post part of that page's code as well. (from where you are coming back to this page... that link or button whatever you've provided)
That should solve your problem.

Now regarding the second thing where you want that the next page should show all users choices.. then well according to me it will be easy if you store them in a table so that you can keep track of the all previous choices of that user....

Regards,
RP
Feb 7 '08 #2

P: 2
Thank you for answering :)

But this is actually the only page i have up until now. The code on this page doesnt send any values, so I didnt start making a new page to catch the values.
Feb 7 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.