Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

i need some help from you :)...when we select 'info' list in the combobox,automatically the article with type 'info' charateristic showed up, and when we select 'Berita' list in combobox,automatically the article with type 'Berita' charateristic showed up.. here is my code... it is a long script T.T & it use post method.. can you help me to make it a little bit simple? using function maybe. thanks before

<form method="post" name="form2">
  <select name="type" id="type">
    <option value="Semua">Semua</option>
    <option value="Berita">Berita</option>
    <option value="Info">Info</option>
  </select>
  <input type="submit" id="type" />
</form>

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

1 Answer

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: <?php echo $show['judul']; ?> change this to read <?php echo htmlspecialchars($show['judul']); ?>. Unless you are very sure that the input is just a number or something that is guaranteed to not contain any of [<>"] this is very important to prevent display bugs or in worst case cross site scripting attacks.

share|improve this answer
thankk youu soo much :D... and thank you for your advice too... :D – strawhat May 20 '12 at 23:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.