karthikanov24 Posted May 20, 2009 Share Posted May 20, 2009 IN THE FOLLOWING PHP FILE(FILE TO DISPLAY CATEGORIES IN ADMIN MODULE OF SHOPPING CART), WHAT IS THE USE OF $i VARIABLE,$class ,'row1'. WHAT IS THE USE OF IF CONDITIONS AT ($i%2), if ($cat_parent_id == 0) { $cat_name = "<a href=\"index.php?catId=$cat_id\">$cat_name</a>"; } if ($cat_image) { $cat_image = WEB_ROOT . 'images/category/' . $cat_image; } else { $cat_image = WEB_ROOT . 'images/no-image-small.png'; } /////////////////////////PHPFILE////////////////////////////// <?php if (!defined('WEB_ROOT')) { exit; } if (isset($_GET['catId']) && (int)$_GET['catId'] >= 0) { $catId = (int)$_GET['catId']; $queryString = "&catId=$catId"; } else { $catId = 0; $queryString = ''; } // for paging // how many rows to show per page $rowsPerPage = 5; $sql = "SELECT cat_id, cat_parent_id, cat_name, cat_description, cat_image FROM tbl_category WHERE cat_parent_id = $catId ORDER BY cat_name"; $result = dbQuery(getPagingQuery($sql, $rowsPerPage)); $pagingLink = getPagingLink($sql, $rowsPerPage); ?> <p> </p> <form action="processCategory.php?action=addCategory" method="post" name="frmListCategory" id="frmListCategory"> <table width="100%" border="0" align="center" cellpadding="2" cellspacing="1" class="text"> <tr align="center" id="listTableHeader"> <td>Category Name</td> <td>Description</td> <td width="75">Image</td> <td width="75">Modify</td> <td width="75">Delete</td> </tr> <?php $cat_parent_id = 0; if (dbNumRows($result) > 0) { $i = 0; while($row = dbFetchAssoc($result)) { extract($row); if ($i%2) { $class = 'row1'; } else { $class = 'row2'; } $i += 1; if ($cat_parent_id == 0) { $cat_name = "<a href=\"index.php?catId=$cat_id\">$cat_name</a>"; } if ($cat_image) { $cat_image = WEB_ROOT . 'images/category/' . $cat_image; } else { $cat_image = WEB_ROOT . 'images/no-image-small.png'; } ?> <tr class="<?php echo $class; ?>"> <td><?php echo $cat_name; ?></td> <td><?php echo nl2br($cat_description); ?></td> <td width="75" align="center"><img src="<?php echo $cat_image; ?>"></td> <td width="75" align="center"><a href="javascript:modifyCategory(<?php echo $cat_id; ?>);">Modify</a></td> <td width="75" align="center"><a href="javascript:deleteCategory(<?php echo $cat_id; ?>);">Delete</a></td> </tr> <?php } // end while ?> <tr> <td colspan="5" align="center"> <?php echo $pagingLink; ?></td> </tr> <?php } else { ?> <tr> <td colspan="5" align="center">No Categories Yet</td> </tr> <?php } ?> <tr> <td colspan="5"> </td> </tr> <tr> <td colspan="5" align="right"> <input name="btnAddCategory" type="button" id="btnAddCategory" value="Add Category" class="box" onClick="addCategory(<?php echo $catId; ?>)"> </td> </tr> </table> <p> </p> </form> Quote Link to comment https://forums.phpfreaks.com/topic/158900-want-explanation/ Share on other sites More sharing options...
Ken2k7 Posted May 20, 2009 Share Posted May 20, 2009 The $i, $class, 'row1', 'row2' and $i % 2 lines are all used to accomplish one thing - alternate rows. So you have a table, and you want alternating colors or whatever for each row. So row1 and row2 are CSS classes for <TR> HTML tag. The variable $i starts at 0. $i % 2 checks if the number is even or odd, but as $i increments, it alternates between the two, which sets $class to either 'row1' or 'row2'. So at first, when $i = 0, %i % 2 == 0, so $class is set to 'row2' because if (0) is false and so it goes to the else clause. See where it says $i += 1? Well that increments $i to be 1. The next time the while loop runs, $i will be 1. $i % 2 == 1. if (1) is true so $class is now 'row1'. Then $i becomes 2. $i % 2 == 0 and it keeps on going like that until the while loop stops. The '%' sign is called a modulus. It means remainder after division. So dividing 1 by 2 gives you a remainder of 1. 2 divided by 2 has a remainder of 0. I hope that helped. Let me know if you need any further clarification. Quote Link to comment https://forums.phpfreaks.com/topic/158900-want-explanation/#findComment-838190 Share on other sites More sharing options...
karthikanov24 Posted May 20, 2009 Author Share Posted May 20, 2009 WHY WE DECLARE $cat_parent_id = 0; WHAT DOES THE EXPRESSIONS AT FOLLOWING MEANS??? if ($cat_parent_id == 0) { $cat_name = "<a href=\"index.php?catId=$cat_id\">$cat_name</a>"; } if ($cat_image) { $cat_image = WEB_ROOT . 'images/category/' . $cat_image; } else { $cat_image = WEB_ROOT . 'images/no-image-small.png'; } Quote Link to comment https://forums.phpfreaks.com/topic/158900-want-explanation/#findComment-838264 Share on other sites More sharing options...
Ken2k7 Posted May 20, 2009 Share Posted May 20, 2009 You mean the one inside the while loop? If so, then one important statement is extract($row). It takes an array and assigns the values to appropriate variable names. One of them is $cat_parent_id. (See the SQL.) It checks if the $cat_parent_id is 0, if so, it gives it a $cat_name. $cat_image is gotten from extract($row) as well. Quote Link to comment https://forums.phpfreaks.com/topic/158900-want-explanation/#findComment-838275 Share on other sites More sharing options...
karthikanov24 Posted May 21, 2009 Author Share Posted May 21, 2009 I MEAN THE $cat_parent_id = 0; OUTSIDE THE WHILE LOOP WHATS THE USE OF IT? Quote Link to comment https://forums.phpfreaks.com/topic/158900-want-explanation/#findComment-838645 Share on other sites More sharing options...
Ken2k7 Posted May 21, 2009 Share Posted May 21, 2009 Probably the same use as inside the while loop. I assume $cat_parent_id is defined somewhere. I honestly can't tell you much in that context. But I guess you can take it down and see what changes. Quote Link to comment https://forums.phpfreaks.com/topic/158900-want-explanation/#findComment-838652 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.