Jump to content

want explanation..


Recommended Posts

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>

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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';

}

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.