/    Sign up×
Community /Pin to ProfileBookmark

dynamic php that changes by option selected

Well I came up with an idea…not saying its a good one, but one none-the-less. Now I do know that this can easily be done by creating multiple pages that have the proper code I was wondering if it would be
possible to have a single php file that would be able to change on the fly by the press of a menu button.

For example:

MySQL database

Table = Books
Field1 = Title
Field2 = Genre

PHP file

Menu with two options: Fiction and Non-Fiction

When fiction is selected the page will display all rows from the database that has fiction in the genre field.

Then when non-fiction is selected the page will then display only the rows that have non-fiction in the genre field.

I know that the following sections in the file would need to change, just not sure how to make it work.

[code=php]<?php

include’config.php’;
mysql_connect(“$host”, “$username”, “$password”) or die(mysql_error());
mysql_select_db(“$db_name”) or die(mysql_error());

// Change the LIKE ‘% %’ when menu button is selected
$result = mysql_query(“SELECT * FROM books WHERE genre LIKE ‘%fiction or non-fiction%’ ORDER BY title ASC”);

?>

<html>
<head>
<!–Change when menu option is selected–>
<title>Fiction or Non-Fiction</title>
</head>

<!–Change when menu option is selected–>
<body id=”fiction or non-fiction”>

<div id=”menu”>
<!– Not sure how to make this work–>
<ul>
</li><a href=”????” target=”_parent”>Fiction</a></li>
</li><a href=”????” target=”_parent”>Non-Fiction</a></li>
</ul>
</div

<?php
while($row=mysql_fetch_array($result))
{
echo “”.$row[‘title’].””;
}
?>

</body>
</html>

[/code]

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@WolfShadeFeb 03.2012 — First of all, if you can upgrade from mysql_connect to mysqli_connect, do it. I think mysql_connect is outdated.

Secondly, you don't have to change the PHP code of the page, itself, in order to do what you want. You can use either links (as you have) or a SELECT for the Fiction/Non-Fiction, and use an AJaX function to make a call to a .php page that runs the query with the value of whatever is clicked/selected, and dynamically refresh the data without reloading the page.
×

Success!

Help @Sno42 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...