mysql - Drop-Down Menu PHP -
i have html code menu sub-menu:
<li class="has-submenu"> <a href="#">services</a> <div class="mainmenu-submenu"> <div class="mainmenu-submenu-inner"> <div> <h4>home</h4> <ul> <li><a href="#">plumbers</a></li> <li><a href="#">painters</a></li> </ul> <h4>people</h4> <ul> <li><a href="#">babysitter</a></li> <li><a href="#">trainer</a></li> </ul> <h4>school</h4> <ul> <li><a href="#">teacher</a></li> </ul> </div> <div> <h4>machine</h4> <ul> <li><a href="#">mecanic</a></li> </ul> </div> </div> </div> </li>
then have table called "services":
id | name | service | description 1 mario plumber aaa 2 luigi plumber aaa 3 link painter aaa 4 zelda babysitter aaa 5 sonic trainer aaa 6 marth teacher aaa 7 ike trainer aaa 8 little mac mecanic aaa
i want create code shows me results associated sub-menu. example, if press plumbers in sub-menu, want show me plumbers table. php code use is:
$query = "select * services service service"; $result = mysql_query($query) or die(mysql_error()); $casaarray = array();
but can show me services. i'm new php.
thanks help.
you can create link in menu:
<a href="information.php?action=plumber">plumbers</a>
then, information.php. (i've used mysql_real_escape_string() function prevent sql injections)
if(!empty($_get["action"])){ $action = mysql_real_escape_string($_get["action"]); $query = "select * services service='$action'"; $result = mysql_query($query); while($row = mysql_fetch_array($result)){ echo $row["name"]." - ".$row["description"]."<br>"; } /* end of while loop */ } /* end of if not empty action */
note:
- you must have structured database, schema have, etc. in order achieve want. can update post in order people here in you.
- as far can see given code, user click link prefer, , redirected page contains information, in form of list or sort.
- if don't have yet code connect database, assume want use php , mysql because of tag used in post. unfortunately, mysql deprecated , recommend use mysqli prepared statement.
to start, have menu, links redirect them page. unfortunately, again, gave 1 table contains information want show on next page.
first, let create table, call main_services
, have 2 column: service_id
(auto-increment , primary key) , service
.
service_id | service 1 | plumber 2 | painter 3 | babysitter 4 | trainer 5 | teacher 6 | mechanic , on...
then, let alter table structure of services
table. services table still have 4 columns: id
, service_id
, name
, description
. index main_services.service_id
services.serviceid
id | service_id | name | description 1 1 mario aaa 2 1 luigi aaa 3 2 link aaa 4 3 zelda aaa 5 4 sonic aaa 6 5 marth aaa 7 4 ike aaa 8 6 little mac aaa
after have established tables, create links on menu.
<div> <h4>options</h4> <ul> <?php $con = new mysqli("yourhost", "username", "password", "database"); /* replace necessary data */ if (mysqli_connect_errno()) { printf("connect failed: %s\n", mysqli_connect_error()); exit(); } if($stmt = $con->prepare("select service_id, service main_service")){ $stmt->execute(); $stmt->bind_result($serviceid,$service); while($stmt->fetch()){ ?> <li> <a href="information.php?id=<?php echo $serviceid; ?>"><?php echo $service; ?></a> </li> <?php } $stmt->close(); } ?> </ul> </div>
then let create information.php file:
<?php /* let first establish connection database */ $con = new mysqli("yourhost", "username", "password", "database"); /* replace necessary data */ if (mysqli_connect_errno()) { printf("connect failed: %s\n", mysqli_connect_error()); exit(); } if(!empty($_get["id"])){ if($stmt = $con->prepare("select name, description services service_id = ?")){ ?> <table> <tr> <th>name</th> <th>description</th> </tr> <?php $stmt->bind_param("i",$_get["id"]); $stmt->execute(); $stmt->bind_result($name,$description); while($stmt->fetch()){ ?> <tr> <td><?php echo $name; ?></td> <td><?php echo $description; ?></td> </tr> <?php } /* end of while loop */ ?> </table> <?php $stmt->close(); } /* end of prepared statement */ } /* end of if not empty id */ ?>
Comments
Post a Comment