Present database results based on form submission When we select the 'info' list in the select field, the article with type 'info' characteristic automatically showed up, and when we select 'Berita' list in the select field, the articles with type 'Berita' characteristic automatically show up.
This is a long script that uses a post method.  Can you help me make it simpler, perhaps using functions?
&lt;form method=&quot;post&quot; name=&quot;form2&quot;&gt;
  &lt;select name=&quot;type&quot; id=&quot;type&quot;&gt;
    &lt;option value=&quot;Semua&quot;&gt;Semua&lt;/option&gt;
    &lt;option value=&quot;Berita&quot;&gt;Berita&lt;/option&gt;
    &lt;option value=&quot;Info&quot;&gt;Info&lt;/option&gt;
  &lt;/select&gt;
  &lt;input type=&quot;submit&quot; id=&quot;type&quot; /&gt;
&lt;/form&gt;

&lt;table width=&quot;200&quot; border=&quot;1&quot; class=&quot;tbl_art_content&quot; id=&quot;results&quot;&gt;
&lt;tbody&gt;
  &lt;tr&gt;
  &lt;/tr&gt;
    &lt;?php
        $i=0;
        $no=0;
            if($_POST['type'] == 'Semua')
            {
                $query_art = mysql_query(&quot;SELECT * FROM artikel_tbl ORDER BY id&quot;) or die(mysql_error());
            }
            else if ($_POST['type'] == 'Berita')
            {
                $query_art = mysql_query(&quot;SELECT * FROM artikel_tbl WHERE type = 'Berita' ORDER BY id&quot;) or die(mysql_error());
            }
            else if ($_POST['type'] == 'Info')
            {
                $query_art = mysql_query(&quot;SELECT * FROM artikel_tbl WHERE type = 'Info' ORDER BY id&quot;) or die(mysql_error());
            }
            else
            {
                $query_art = mysql_query(&quot;SELECT * FROM artikel_tbl ORDER BY id&quot;) or die(mysql_error());
            }
            while($show=mysql_fetch_array($query_art))
            { 
                $no++;
                if(($no%2)==0)
                    $color = '#f2f2f2'; 
                else
                    $color = '#f9f9f9';
    ?&gt;
  &lt;tr bgcolor=&quot;&lt;?php echo $color; ?&gt;&quot; class=&quot;rows&quot;&gt;
    &lt;td class=&quot;chk_content&quot;&gt;&lt;input type=&quot;checkbox&quot; name=&quot;checked&lt;?php echo $i; ?&gt;&quot; value=&quot;&lt;?php echo $show['id']; ?&gt;&quot;/&gt;&lt;/td&gt;
    &lt;td class=&quot;no_content&quot;&gt;&lt;?php echo $no; ?&gt;&lt;/td&gt;
    &lt;td class=&quot;middle_content&quot;&gt;&lt;?php echo $show['judul']; ?&gt;&lt;/td&gt;
    &lt;td class=&quot;middle_content&quot;&gt;&lt;?php echo $show['penulis']; ?&gt;&lt;/td&gt;
    &lt;td class=&quot;middle_content&quot;&gt;&lt;?php echo $show['type']; ?&gt;&lt;/td&gt;
    &lt;td class=&quot;middle_content&quot;&gt;&lt;img src=&quot;.././upload/artikel/&lt;?php echo $show['foto']; ?&gt;&quot; width=&quot;144&quot; height=&quot;88&quot;/&gt;&lt;/td&gt;
    &lt;td class=&quot;middle_content&quot;&gt;&lt;?php echo $show['tanggal']; ?&gt;&lt;/td&gt;
    &lt;td class=&quot;aksi_content&quot;&gt;&lt;div id=&quot;aksi_table&quot;&gt;
            &lt;ul&gt;
                &lt;li&gt;&lt;a href=&quot;table_artikel.php?edit=&lt;?php echo $show['id']; ?&gt;&quot;&gt;&lt;img src=&quot;images/Apps-text-editor-icon.png&quot; width=&quot;20&quot; height=&quot;20&quot; /&gt;&lt;/a&gt;&lt;/li&gt;
                &lt;li&gt;&lt;a href=&quot;table_artikel.php?delete=&lt;?php echo $show['id']; ?&gt;&quot; onclick=&quot;return confirm('Hapus artikel?')&quot;;&gt;&lt;img src=&quot;images/Remove-icon.png&quot; width=&quot;20&quot; height=&quot;20&quot;/&gt;&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;
        &lt;/div&gt;&lt;!-- end of aksi_table --&gt;&lt;/td&gt;
  &lt;/tr&gt;
    &lt;?php
        $i++; 
        }
    ?&gt;
    &lt;input type=&quot;hidden&quot; name=&quot;n&quot; value=&quot;&lt;?php echo $i; ?&gt;&quot; /&gt; 
    &lt;/tbody&gt;
&lt;/table&gt;
Well... First we can shorten your if-construct a lot:

$sql = 'SELECT * FROM artikel_tbl';
if ($_POST['type'] == 'Info' || $_POST['type'] == 'Berita')
  $sql .= " WHERE type = '{$_POST['type']}'";
$sql .= ' ORDER BY id';


In my opinion this also increases readability, because it is clear that the query is basically everytime the same, you are just including a condition in one case.

Next thing the if(($no%2)==0)-part. Do you know about the nth-child of css? Here is an example for that: http://jsfiddle.net/2ZR2U/

For these things: &lt;?php echo $show['judul']; ?&gt; change this to read &lt;?php echo htmlspecialchars($show['judul']); ?&gt;. Unless you are very sure that the input is just a number or something that is guaranteed to not contain any of [&lt;>"] this is very important to prevent display bugs or in worst case cross site scripting attacks.