mysql - how to show fixed images on pageload in php?

915

I have a php page where user can select one category via dropdown list and after clicking 'go' button the relative images for that category will display on page below the dropdown list from database. This works fine. Now I want to display the images on page load for the 1st dropdown list category everytime then after based on user choice. I think my point is clear to all. for example, 1st category in the dropdown is Featured art, on loading the php page images related to Featured art always display on page. After that user can change it by changing the category from dropdown. Help me please. Here is my complete code :

 <body>
 <?php
 include ('connect-db.php');
 ?>

<form name="product" method="post" action="">
<table align="right" width="10%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>Category</td>
<td>
<select name="category">

 <?php
 $sql = "SELECT id, art_name FROM category;";
 $result = mysql_query($sql);
 while ($row = mysql_fetch_assoc($result)) {
?>

<option value="<?= $row['id']; ?>"><?= $row['art_name']; ?></option>


<?php } ?>
</select>
</td>
</tr>
<tr>
 <td>&nbsp;</td>
 <td><input name="go" type="submit" value="Go" /></td>
 </tr>
  </table>
 </form>

 <div align="center">

 <ul >
 <?php
  $id = (int)$_POST['category'];
  $sql_search = "SELECT id, categoryid, path FROM list WHERE categoryid = $id";
 $search = mysql_query($sql_search);
  if (isset($_POST['go'])) {
  while ($row = mysql_fetch_assoc($search)) {
 ?>


 <li><a href="<?= $row['path']; ?>" onclick="return hs.expand(this)"><img src="<?= $row['path']; ?>" border="0"></a></li>
 <?php }

 }

 else {

  }


  ?>
  </ul>
  </div>
  </body>

Thanks in advance!:)

293

Answer

Solution:

You may try to useGET request instead ofPOST. Using this you may check like:

<?php

$get_id = $_GET['category'];
if($get_id == null){
    $get_id = <default-value-say-1>;
}
$id = (int)$get_id;

?>

Also, in the dropdown, set the default / first category selected.

When the user selects another option, redirect to url?category=<selected-id>

This is not a copy-paste solution, please take this as a starting point and follow the best-practices for development in PHP or any PHP framework of your choice.

Hope this helps!

Vivek

People are also looking for solutions to the problem: php - call controller from javascript on view using codeigniter

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.