-2

I have this html code for a menu with 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 I have this 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 to create a code that shows me the results only associated with that sub-menu. For example, if I press Plumbers in the sub-menu, I want it to only show me the plumbers from my table. The PHP code I use is:

$query = "SELECT * FROM services WHERE service LIKE service";
                    $result = mysql_query($query) or die(mysql_error());
                    $casaArray = array();

But I can only get it to show me all the services. I'm new to PHP.

Thanks for your help.

Alien
  • 29
  • 5
  • Sounds good. What is the problem? – jeroen Jun 15 '15 at 08:40
  • Can you give us the php code that you are working at? And as @jeroen says, what is the problem with it? Kindly update your post. – Logan Wayne Jun 15 '15 at 08:45
  • StackOverflow is about specific development question. It's not about asking for someone do do the coding for you. Show some effort and then ask questions if you can't figure something out. – martynasma Jun 15 '15 at 08:59
  • Done.When I use this code, all the services appear. – Alien Jun 15 '15 at 08:59

2 Answers2

1

You can simply create a link in your menu:

<a href="information.php?action=Plumber">Plumbers</a>

Then, for your information.php. (I've used mysql_real_escape_string() function to prevent SQL injections)

if(!empty($_GET["action"])){

  $action = mysql_real_escape_string($_GET["action"]);

  $query = "SELECT * FROM services WHERE 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 a structured database, a schema that you have, etc. in order to achieve what you want. You can update your post in order for people here in SO to help you.
  • As far as I can see with your given code, user will click a link that they prefer, and they will be redirected to a page that contains information, in a form of list or some sort.
  • If you don't have yet a code that would connect to your database, I assume that you want to use PHP and mysql because of the tag you used in your post. Unfortunately, mysql is already deprecated and we recommend that you use mysqli prepared statement.

To start, you have this menu, with links that would redirect them to another page. Unfortunately, again, you only gave us one table that contains the information you want to show on the next page.

First, let us create a table, which we will call main_services and it will have two column: service_id (auto-increment and your primary key) and service.

service_id  |  service
    1       |  Plumber
    2       |  Painter
    3       |  Babysitter
    4       |  Trainer
    5       |  Teacher
    6       |  Mechanic
and so on...

Then, let us alter the table structure of your services table. Your services table will still have four columns: id, service_id, name and description. We will index the main_services.service_id to your 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 we have established your tables, we will create the links on your 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 FROM 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 us create your information.php file:

<?php

  /* LET US FIRST ESTABLISH YOUR CONNECTION TO YOUR 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 FROM services WHERE 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 */

?>
Community
  • 1
  • 1
Logan Wayne
  • 6,001
  • 16
  • 31
  • 49
  • thanks so much. Just two questions: 1. Where I have to create to links...Do I create for each
  • ? 2. Do I have to use the "include()" in any of my files?
  • – Alien Jun 15 '15 at 10:47