I want to calculate average rating of all heading. I have 6 heading under one person and every person have 5 star. I want to show over all rating (e.g 4.2 or 4 )
Here is my code
<li><p>Overall Experience</p>
<?php
$starsLeft = 5 - $count_all_guest_star[0]->overall_experience_star;
if($count_all_guest_star[0]->overall_experience_star>0):
for( $i=1; $i<= $count_all_guest_star[0]->overall_experience_star; $i++)
{ ?>
<img src="<?php echo base_url(); ?>assets/img/on-stars.gif"/>
<?php
}
endif;
if ($starsLeft > 0) { // if there are any more stars left
for ($i = 1; $i <= $starsLeft; $i++) { // go through each remaining star
// show it empty
?>
<img src="<?php echo base_url(); ?>assets/img/off-stars.gif"/>
<?php }
}
?>
</li>
<li><p>Communication</p>
<?php
$com_starsLeft = 5 - $count_all_guest_star[0]->communication_star;
if( $count_all_guest_star[0]->communication_star > 0):
for( $j=1; $j<= $count_all_guest_star[0]->communication_star; $j++)
{
?>
<img src="<?php echo base_url(); ?>assets/img/on-stars.gif"/>
<?php
}
endif;
if ($com_starsLeft > 0) { // if there are any more stars left
for ($i = 1; $i <= $com_starsLeft; $i++) { // go through each remaining star
// show it empty
?>
<img src="<?php echo base_url(); ?>assets/img/off-stars.gif"/>
<?php }
}
?></li>
$AverageRating = ((1*$starsLeft)+(2*$com_starsLeft)+(3*$acc_starsLeft)+(4*$clean_starsLeft)+(5*$pick_starsLeft) + (6*$pick_starsLeft))/6;
echo '$AverageRating';
kindly advice me any solution.
I think you can compute the average like this
$avg = ($count_all_guest_star[0]->overall_experience_star +
$count_all_guest_star[0]->communication_star +
$count_all_guest_star[0]->heading3 +
$count_all_guest_star[0]->heading4 +
$count_all_guest_star[0]->heading5 +
$count_all_guest_star[0]->heading6) / 6;
You can then use the same method to display stars.
$avg_stars_left = 5 - $avg;
Related
The code I have works absolutely perfectly:
<?
$maxresults = 10;
$total = $row[total];
$pagecount = $total / $maxresults;
if (!isset($_GET['page'])) { $_GET['page'] = '1'; }
$startcount = (($_GET['page'] - 1) * $maxresults + 1);
$stopcount = $startcount + ($maxresults - 1);
$lastTime = null;
$i='0';
$sc = $startcount;
$stmt=$db->prepare("// SELECT STATEMENT");
$stmt->bindParam(':user', $username);
$stmt->execute();
while ( $row = $stmt->fetch() ) {
if (!($sc > $stopcount)) {
$i++;
if( $row['time'] !== $lastTime ) {
if ( $lastTime === NULL ) {
echo '// close of loop';
} else {
echo '// loop grouping as title';
}
$lastTime = $row['time'];
}
if ($i >= $sc) { ?>
// the html to loop
<?
$sc++;
}
}
}
?>
Forward and Back buttons are easy enough to create. if $_GET['page'] * 10 > $total, active the next button, if $_GET['page'] > 1 activate previous button.
What I can't figure out is the correct formula and loop so I can always display the correct number of "page numbered" links. I want to always show 5 based on the 3rd number. So 1 would should 5 links: 1 - 5, 2 would also show 1 - 5, as would 3, 4 would show 2 - 6, 5 would show 3 - 7.
Now obviously, I could just add 2 and subtract 2 from the current $_GET[page] if the get page is greater than 3, but I need to take into account testing to see if the results go that high. So only if results are greater than 10 should number 2 show, and then if on page 16 pages 17 and 18 should only show if there are 170 and 180 results to display.
Perhaps I have just been staring at the code to long, but I can't seem to get a formula or loop correct in my head to even attempt the code page.
It was a fairly specific question. Wasn't sure if anyone would help with something so specific or not. Guess I learned.
Here is the answer to my own question:
<ul>
<li class="<? if ($_GET['page'] < '2') { echo 'disabled' ; } ?>">
<a href="<?
if ($_GET['page'] == '2') {
echo 'inbox.php';
} else {
echo '?page='.($_GET[page] - 1);
}
?>">Prev</a>
</li> <!-- previous button-->
<li style="<? if ($_GET['page'] - 2 < '1') { echo 'display:none;'; } ?>">
<a href="?page=<?
echo ($_GET[page] -2); ?>">
<? echo ($_GET[page] -2); ?>
</a>
</li> <!-- the button 2 before current spot if its greater than 0 -->
<li style="<? if ($_GET['page'] - 1 < '1') { echo 'display:none;'; } ?>">
<a href="?page=<?
echo ($_GET[page] -1); ?>">
<? echo ($_GET[page] -1); ?>
</a>
</li> <!-- the button 1 before current spot if its greater than 0 -->
<li class="disabled">
<? echo $_GET[page]; ?>
</li> <!-- current button disabled -->
<li style="<? if ($total - ($_GET[page] * 10) <= '0') { echo 'display:none;'; } ?>">
<a href="?page=<?
echo ($_GET[page] + 1); ?>">
<? echo ($_GET[page] +1); ?>
</a>
</li> <!-- the button 1 after current spot if results go that high -->
<li style="<? if ($total - ($_GET[page] + 1) * 10 <= '0') { echo 'display:none;'; } ?>">
<a href="?page=<?
echo ($_GET[page] + 2); ?>">
<? echo ($_GET[page] + 2); ?>
</a>
</li> <!-- the button 2 after current spot if results go that high -->
<li class="<? if ($total - ($_GET[page] * 10) <= '0') { echo 'disabled'; } ?>">
Next
</li> <!-- the next button if results go that high -->
</ul>
#bruce after seeing your solution, I'd like to give you my own solution.
$page = $_GET['page'];
$max_pages = ceil($total / $maxresults);
$min_pages = 1;
$start = max($min_pages, $page-2);
$end = min($max_pages, $start+2);
$nav = ($page > $min_pages ? '<li><</li>' : '' );
for ($i = $start; $i <= $end; $i++)
$nav .= '<li' . ($i == $page ? 'class="active"' : '' ) . '>' . $i . '</li>';
$nav .= ($page < $max_pages ? '<li>></li>' : '' );
How do I merge two (for) in one code?
e.g first code
for ($i = 0, $n = count($options); $i < $n; $i ++) {}
and second code
for ($u = 'rel_article1'; $u <= 'rel_article5'; $u++)
here is oll code
<?php if ($display_poll) { ?>
<form action="<?php echo JRoute::_('index.php');?>" method="post" name="poll_vote_<?php echo $poll->id;?>" id="poll_vote_<?php echo $poll->id;?>">
<?php for ($i = 0, $n = count($options); $i < $n; $i ++) { ?>
<label for="mod_voteid<?php echo $options[$i]->id;?>" class="<?php echo $tabclass_arr[$tabcnt].$params->get('moduleclass_sfx'); ?>" style="display:block; padding:2px;">
<input type="radio" name="voteid" id="mod_voteid<?php echo $options[$i]->id;?>" value="<?php echo $options[$i]->id;?>" alt="<?php echo $options[$i]->id;?>" <?php echo $disabled; ?> />
<?php echo $options[$i]->text; ?> <?php for ($u = 'rel_article1'; $u <= 'rel_article5'; $u++) { ?>
<a href="<?php echo $params->get($u); ?>" onClick="return popup(this, 'notes')">
<img src="/images/stories/add.png" alt="play"></a>
<?php
} ?
I want in one poll show onle play button
now shows this
here is my xml code:
You don't really need that second for. You already have an index with $i from your first loop. (Except that it starts from 0 instead of 1, but you can easily get it working)
Remove the second for, and try this :
// echo $params->get($u);
echo $params->get('rel_article' . ($i + 1));
This while loop is supposed to continue until the end of the array or until counter reaches 6, what am I doing wrong here?
while($row = mysql_fetch_array($resultSet, MYSQL_ASSOC) && ($counter < 6))
{
?>
<?php echo $row['Item_NAME'] ?>
<img style="width:250px; height:250px;" src="<?php {echo "{$row['Item_IMAGE']}";} ?>">
<?php $counter = $counter + 1;
?>
<?php
}
Works without the && ($counter < 6)but shows the wrong number of images, adding this will show the correct number of boxes (where the images should be) but not retrieve the images or names from the array.
Thanks for any help.
Couldn't you just use break; inside, when the second condition isn't true anymore?
while($row = mysql_fetch_array($resultSet, MYSQL_ASSOC))
{
if($counter > 5){
break;
}
?>
<?php echo $row['Item_NAME'] ?>
<img style="width:250px; height:250px;" src="<?php {echo "{$row['Item_IMAGE']}";} ?>">
<?php $counter = $counter + 1;
?>
<?php
}
At the moment, with the code from below, I have the data shown like this:
http://img27.imageshack.us/img27/8083/29769986.jpg
but I want it to be shown like this:
http://img259.imageshack.us/img259/3233/24033830.jpg
The code for the data shown, as it is on the first image, is:
<div id="content">
<?php foreach ($categories as $category) { ?>
<div class="manufacturer-list">
<div class="manufacturer-heading"><?php echo $category['name']; ?><a id="<?php echo $category['name']; ?>"></a></div>
<div class="manufacturer-content">
<?php if ($category['manufacturer']) { ?>
<?php for ($i = 0; $i < count($category['manufacturer']);) { ?>
<ul>
<?php $j = $i + ceil(count($category['manufacturer']) / 4); ?>
<?php for (; $i < $j; $i++) { ?>
<?php if (isset($category['manufacturer'][$i])) { ?>
<li><?php echo $category['manufacturer'][$i]['name']; ?></li>
<?php } ?>
<?php } ?>
</ul>
<?php } ?>
<?php } ?>
</div>
</div>
<?php } ?>
</div>
I order to get the "Hewlett-Packard" text under the "HTC" text, I've changed the "/ 4" into "/ 1", but I have no idea how to make the data to be shown into three columns (like on the second picture), instead of one, as it is now (as shown on the first picture).
Thanks in advance.
EDIT: What I actually need, is to count and to do the calculation on this code:
<?php foreach ($categories as $category) { ?>
.
.
.
<?php } ?>
So it needs to count the number of categoris, do the calculations, and present the code between into three columns.
Try this one.
<div id="content">
<div class="content-column">
<?php
$cols = 3; // Change to columns needed.
$catcount = count($categories);
$catpercol = ceil($catcount / $cols);
$c = 0;
foreach ($categories as $category) {
if ( $c == $catpercol ) {
$c = 0;
print "</div><div class='content-column'>";
}
?>
<div class="manufacturer-list">
<div class="manufacturer-heading"><?php echo $category['name']; ?><a id="<?php echo $category['name']; ?>"></a></div>
<div class="manufacturer-content">
<?php if ($category['manufacturer']) { ?>
<?php for ($i = 0; $i < count($category['manufacturer']);) { ?>
<ul>
<?php $j = $i + ceil(count($category['manufacturer']) / 4); ?>
<?php for (; $i < $j; $i++) { ?>
<?php if (isset($category['manufacturer'][$i])) { ?>
<li><?php echo $category['manufacturer'][$i]['name']; ?></li>
<?php } ?>
<?php } ?>
</ul>
<?php } ?>
<?php } ?>
</div>
</div>
<?php $c++; } ?>
</div>
</div>
Add .content-column { float: left; width: 33.33333%; } to your CSS.
Details:
$cols = 3; enables you to set the desired number of columns (note: you might need to change CSS accordingly).
$catcount = count($categories); gives you the total number of categories about to be rendered.
$catpercol = ceil($catcount / $cols); divides that total number evenly into the required number of columns with the last column having eventually less items than the others.
$c = 0; is your counter. It increases at the end of the outer foreach loop.
Within the loop, $cis checked if it matches the $catpercol number and if so, the current parent div is closed and a new one created. You end up with as many parent divs as you need columns. Just add appropiate CSS to make them appear besides each other.
understand the following code that according to your requirement, the code just give the hint to achieve that you want according to your given screen shoot http://img259.imageshack.us/img259/3233/24033830.jpg
echo "<table>";
echo "<tr>";
$i = 1;
do{
// column range
$range = 3;
echo "<td>" . $i;
if( $i % $range == 0 ){
echo "</tr>";
echo "<tr>";
}
echo "</td>";
$i++;
}while( $i <= 10 );
echo "</tr>";
echo "</table>";
Hope this will help you
I think it is possible to create three column layout via html/css and without any change of your PHP code. Just use float:left; width: 33%. You can also use absolute value for width property because of margins and borders.
I have a code:
<?php for ($i = 0; $i < sizeof($categories); $i = $i + 4) { ?>
<?php for ($j = $i; $j < ($i + 4); $j++) { ?>
<?php if (isset($categories[$j])) { ?>
<img src="<?php echo $categories[$j]['thumb']; ?>" title="<?php echo $categories[$j]['name']; ?>" alt="<?php echo $categories[$j]['name']; ?>" style="margin-bottom: 3px;" /><br />
<?php echo $categories[$j]['name']; ?>
<?php } ?>
<?php } ?>
<?php } ?>
I want to place these categories in 2-column like this:
<div class="span-8">
<div class="product">
product1
</div>
</div>
<div class="span-8 last">
<div class="product">
product2
</div>
</div>
How can I do this?
<?php
for ($i = 0; $i < sizeof($categories); $i = $i + 4) {
for ($j = $i; $j < ($i + 4); $j++) {
if (isset($categories[$j])) {
if($colcount % 2){
$col1+="<div class='product'><a href='".$categories[$j]['href']."'><img src='".$categories[$j]['thumb']."' title='".$categories[$j]['name']."' alt='".$categories[$j]['name']."' style='margin-bottom: 3px;' /></a></div><a href='".$categories[$j]['href']."'>".$categories[$j]['name']."</a>";
}else{
$col2+="<div class='product'><a href='".$categories[$j]['href']."'><img src='".$categories[$j]['thumb']."' title='".$categories[$j]['name']."' alt='".$categories[$j]['name']."' style='margin-bottom: 3px;' /></a></div><a href='".$categories[$j]['href']."'>".$categories[$j]['name']."</a>";
}
$colcount++;
}
}
}
echo "<div class='span-8'>".$col1."</div><div class='span-8 last'>".$col2."</div>";
Or you could do it the easy way and make it a fluid <ul> that way they would do it automatically
I don't really see the correlation between the php and the html code, but I think you just need to output a different css class for each odd category:
$last = false;
foreach($categories as $c) {
//Output category html
?>
<div class="span <?=($last)?'last':''?>">.....</div>
<?
$last != $last;
}