show the total count value at beginning - php

I have no experience in php but i am facing a problem in php.
i have a php code like this:
<div>
<ul>
<li><input type="checkbox" name="checkAll"
id="selectAll" class="checkbox"> Select All </li>
<?php
$count = 0;
foreach ($caseList as $testcase)
{
?>
<li><input type="checkbox" name="testcases"
class="checkbox"
value="<?php echo $testcase->id; ?>">
<?php echo $testcase->name; ?>
<?php $count++;?>
</li>
<?php
}
?>
<li> <?php echo "Total Test Case COunt: $count";?> </li>
</ul>
But i want to print "Total Test Case count" at beginning list. How i can do this?

Put a count($caseList) at the top.
<div>
<ul>
<li><input type="checkbox" name="checkAll" id="selectAll" class="checkbox"> Select All </li>
<li> <?php echo "Total Test Case COunt: " . count($caseList);?> </li>
<?php
$count = 0;
foreach ($caseList as $testcase)
{
?>
<li><input type="checkbox" name="testcases" class="checkbox" value="<?php echo $testcase->id; ?>"><?php echo $testcase->name; ?><?php $count++;?></li>
<?php
}
?>
</ul>

You can try to get the length of the array without a counter using the count function.
$count = count($caseList);
So your final code could be like
div>
<ul>
<li><input type="checkbox" name="checkAll" id="selectAll" class="checkbox"> Select All </li>
<?php
$total = count($caseList);
echo "Total Test Case Count: $total"
$count = 0;
foreach ($caseList as $testcase)
{
?>
<li><input type="checkbox" name="testcases" class="checkbox" value="<?php echo $testcase->id; ?>"><?php echo $testcase->name; ?><?php $count++;?></li>
<?php
}
?>
</ul>

you need to move increment from "li" tag
<div>
<ul>
<li><input type="checkbox" name="checkAll"
id="selectAll" class="checkbox"> Select All </li>
<?php
$count = 0;
foreach ($caseList as $testcase)
{
?>
<li><input type="checkbox" name="testcases"
class="checkbox"
value="<?php echo $testcase->id; ?>">
<?php echo $testcase->name; ?>
</li>
<?php
$count++;
}
?>
<li> <?php echo "Total Test Case COunt: $count";?> </li>
</ul>

Related

how to access php variable out side of a foreach loop

i have a veriable named (num) which is used for increments changes names of ids by 1 num+1 but in my foreachloop i cant access it .
i tried declaring it before the loop still doesnot work
<?php $num = 0; ?>
<?php foreach($listings as $list):?>
<li>
<div class="checkbox">
<input type="checkbox" class="css-checkbox" value="<?php echo $list['title'];?>" id="visit<?php echo $num+1;?>" name="treatment<?php echo $num+1;?>">
<label for="visit<?php echo $num+1;?>" class="css-label"><?php echo $list['title']?> <strong><?php echo $list['price'];?><span>£</span></strong></label>
</div>
</li>
<?php endforeach; ?>
</ul>
<hr>
<input type="hidden" value="<?php echo $num;?>" name="total"/>
i want the input ids to be incremented by 1 like treatment1,treatment2
You should increment the $num variable by doing $num++; once inside the loop, then print it where you need it with <?php echo $num; ?> without using <?php echo $num+1; ?> - as doing so will only increment it as you echo it - not add one to each iteration.
<?php
$num = 0;
foreach($listings as $list):
$num++; // Increment $num for each iteration
?>
<li>
<div class="checkbox">
<input type="checkbox" class="css-checkbox" value="<?php echo $list['title'];?>" id="visit<?php echo $num;?>" name="treatment<?php echo $num;?>">
<label for="visit<?php echo $num;?>" class="css-label"><?php echo $list['title']?> <strong><?php echo $list['price'];?><span>£</span></strong></label>
</div>
</li>
<?php endforeach; ?>
If your $listings is numeric indexed, you can use the key of each element in the array instead by doing
foreach($listings as $num=>$list):
?>
<li>
<div class="checkbox">
<input type="checkbox" class="css-checkbox" value="<?php echo $list['title'];?>" id="visit<?php echo $num;?>" name="treatment<?php echo $num;?>">
<label for="visit<?php echo $num;?>" class="css-label"><?php echo $list['title']?> <strong><?php echo $list['price'];?><span>£</span></strong></label>
</div>
</li>
<?php endforeach; ?>
The problem is you did not set the value of $num variable you just only print or echo it inside the html tags. You need to add or increment the $num variable inside the loop like this.
<?php $num++; ?>
or
<?php $num = $num+1; ?>

generating tabs and tables within multiple while loop

I am trying to generate Tabs from 1st while loop and within that table from second while loop.
I will fetch the records date wise from my 1st table ie, treatment from that i am generating Tab, In another table called treatment_litems i have stored all the line items for treatment table records. So for 1st date (Tab) from treatment table, i want to display all the related records from treatment_litems in table format.
I am getting records but Tabs are not getting added, but everytime new Tabs are generating.
here is my code
<ul class="nav nav-tabs">
<?php $i=1; while($tt2 = mysqli_fetch_array($tt1)) { ?>
<li>
<?php echo $tt2['date']; ?>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane fade active in" id="tab_1_<?php echo $i; ?>">
<?php $l1 = mysqli_query($con, "SELECT * FROM treatment_litems WHERE tid=".$tt2['tid'].""); ?>
<table class="table">
<thead><tr><th>Drugs</th><th>Route</th><th>Dosage</th></tr></thead>
<tbody>
<?php
while($l2 = mysqli_fetch_array($l1)) { ?>
<tr><td><?php echo $l2['drugs']; ?></td>
<td><?php echo $l2['route']; ?></td>
<td><?php echo $l2['dosage']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
<?php $i++ ; } ?>
</div>
Here is the image
**** EDITED ******
<ul class="nav nav-tabs">
<?php
while($tt2 = mysqli_fetch_array($tt1)) { ?>
<li>
<?php echo $tt2['date']; ?>
</li>
</ul>
<?php } ?>
<div class="tab-content">
<div class="tab-pane fade active in" id="tab_1_<?php echo $tt2['tid']; ?>">
This is my working code, you can get hints by this:
<div id="tabs" style="float:left">
<ul>
<?php $getlang=mysql_query("select * from language where Mid='$mid' and status='1' order by LanguageName asc");
if(mysql_num_rows($getlang)>0){
while($arr=mysql_fetch_array($getlang)){?>
<li><?php echo $arr['LanguageName']; ?></li>
<?php }}?>
</ul>
<?php $getlang=mysql_query("select * from language where Mid='$mid' and status='1' order by LanguageName asc");
if(mysql_num_rows($getlang)>0){
while($arr=mysql_fetch_array($getlang)){?>
<div id="tabs-<?php echo $arr['LanguageID'] ?>">
<input type="hidden" name="LanguageId[]" class="langID" value="<?php echo $arr['LanguageID']; ?>">
<input type="hidden" name="mid" value="<?php echo $mid; ?>">
<div class="rows">
<label>Notification Text</label><br/>
<textarea rows="3" cols="10" name="description[]" id="" maxlength="1000" style="width:700px; height:100px;" required="required"></textarea>
</div>
</div>
<?php } }
Check and do let me know.

php code to display icon on active button

I am working on cakephp 1.3..
i have button and value displaying inside button coming from database.
I want to show arrow icon on my active button only..
now the active arrow icon is displaying in all button...inside forloop.
plz help me to do this..
below is my code
<?php
foreach($modes as &$mo)
{
$temp = "Road Vs "; $mo = strtolower($mo);
?>
<li class="active">
<input name="data[Customer][mode]" class="railbtn" type="submit" id="mode" value="<?php echo $temp.$mo; ?>">
<span class="arrow">
<?php echo $this->Html->image('red_arrow.png', array('alt' => '')); ?>
</span>
</li>
<?php } ?>
You should add a condition in your for loop
Like this
<?php
// you need to send button unique name to controller
foreach($modes as &$mo)
{
$temp = "Road Vs "; $mo = strtolower($mo); ?>
<li class="<?php echo ($mo == $selectedUniqueButtonID) ? 'active' : '' ?>">
<input name="data[Customer][mode]" class="railbtn" type="submit" id="mode" value="<?php echo $temp.$mo; ?>">
<?php if($mo == $selectedUniqueButtonID){ ?>
<span class="arrow">
<?php echo $this->Html->image('red_arrow.png', array('alt' => '')); ?>
</span>
<?php } ?>
</li>
<?php
} ?>
Controller code
$selectedUniqueButtonID = $this->data['Customer']['mode'];
$this->set('selectedUniqueButtonID',$selectedUniqueButtonID);

How can I divide/split an array?

I have an array here which has (30 values e.g.).And I displayed it on a div. I want to split them into 3parts and display them in a three separated div. How can I do this one?Here's my code:
<div class="span4">
<?php if($col):?>
<?php foreach($col as $names):?>
<label class="checkbox">
<input type="checkbox" id="chk_distinct" class="check"
value="<?php echo $names->COLUMN_NAME;?>">
<?php echo $names->COLUMN_NAME;?></label>
<?php endforeach;?>
<?php endif;?>
</div>
It will display this one:
<div>
Checkbox1
Checkbox2
Checkbox3
Checkbox4
...
Checkbox30
</div>
I wanted it to look like this one:
<div1> <div2> <div3>
Checkbox1 Checkbox11 Checkbox21
Checkbox2 Checkbox12 Checkbox22
Checkbox3 Checkbox13 Checkbox23
Checkbox4 Checkbox14 Checkbox24
Checkbox5 Checkbox15 Checkbox25
... ... ...
Checkbox10 Checkbox20 Checkbox30
</div> </div> </div>
Any Idea about this one?Beginner here..
Use array_chunk For example
$cols = array_chunk($col, 10, true);
foreach ($cols as $col)
{
echo '<div>';
foreach ($col as $names)
{
echo '<label>'.$names->COLUMN_NAME.'</label>';
}
echo '</div>';
}

Code logic not computing correct data

I'm desperately hoping someone can assist me with this error. Admittedly, I'm a novice and I've tried for literally 60+ hours to fix this but I just can't get there.
Right, here goes.
I have a football league table of 20 teams. It works, it's great, happy days. However, I needed to reduce it to 12 teams. A problem because I didn't create the website, someone else did a while back.
So I've needed to amend a table with 20, to 12. That's all.
Daunted, I rolled up my sleeves scoured the code and located this bit;
<?php
$query = $SITE -> Query ( 'SELECT a.*, b.name, b.colour, b.txtcolour,
(sum((a.wins*3) + (a.draws*1) - a.pts)) as points,
(sum(a.wins + a.losses + a.draws)) as pld,
(sum(a.glsfor - a.glsaga)) as gd
FROM got_standings a
inner join got_clubs b on (a.cid = b.id)
WHERE a.season = "'.$url[2].'"
GROUP BY a.cid
ORDER BY points DESC, gd DESC, glsfor DESC' );
if ($SITE -> Count ($query) == 0) {
$loop = 0;
while($loop <= 19) {
++$loop;
?>
After some trial and error in those 60 hours I've been trying to resolve, I changed;
while($loop <= 19)
to;
while($loop <= 11)
And low and behold, the system now prompted me to enter 12 teams into a league table, as oppose to 20. Superb. Or so I thought...
When I first enter the data into the table and 'submit', it works - as it did perfectly when 20 teams where listed before the above change. However, when I 'update'/make any continual changes to the league table, it also calculates the previous
So for 'Port Talbot Town' I enter "1 Win", "1 Draw", "1 Loss", "5 Goals Scored" "5 Goals Conceded"
And it works. It calculates 3 games played and 4 points (3 for a win, 1 for a draw, 0 for a loss)as shown here.
BUT, when I then enter other information, so entering the same information for 'Aberystwyth Town'and 'update', the information for the previous data (Port Talbot Town) is calculated again?as shown here.
I'm at complete loss. I'd be incredibly grateful and appreciative for any assistance.
The entire code is pasted at the bottom of this.
Please guys! Help an idiot out! 8)
<?php
}
} else if ($url[3] == 'league') {
if ($url[4] == 'standings') {
?>
<ul class="buttonbar">
<li class="button">Add Fixture</li>
<li class="button">Standings</li>
</ul>
<form action="<?=$CONF['site']; ?>mcms/applications/fixtures/submit.php?do=fixtures&action=standings&season=<?=$url
[2]; ?>&comp=<?=$url[3]; ?>" method="post" name="standings">
<div class="table">
<div class="th">Welsh Premier League Standings</div>
<ul class="tr shadow">
<li class="td-squadno">Pos.</li>
<li class="td4" style="width: 170px;">Team</li>
<li class="td4">PLD</li>
<li class="td4">W</li>
<li class="td4">D</li>
<li class="td4">L</li>
<li class="td4">+</li>
<li class="td4">-</li>
<li class="td4">PTS</li>
</ul>
<?php
$query = $SITE -> Query ( 'SELECT a.*, b.name, b.colour, b.txtcolour,
(sum((a.wins*3) + (a.draws*1) - a.pts)) as points,
(sum(a.wins + a.losses + a.draws)) as pld,
(sum(a.glsfor - a.glsaga)) as gd
FROM got_standings a
inner join got_clubs b on (a.cid = b.id)
WHERE a.season = "'.$url[2].'"
GROUP BY a.cid
ORDER BY points DESC, gd DESC, glsfor DESC' );
if ($SITE -> Count ($query) == 0) {
$loop = 0;
while($loop <= 11) {
++$loop;
?>
<ul class="tr" id="team_<?=$loop; ?>">
<li class="td-squadno"><?=$loop; ?></li>
<li class="td2" style="width: 170px;">
<?php
echo ( '
<select name="cid[]">
' );
$sql = $SITE -> Query ( 'SELECT id, name FROM got_clubs WHERE country = "Wales" ORDER BY name ASC' );
while($club = $SITE -> FetchArray($sql)) {
echo ( '<option value="'.$club['id'].'"' );
if ($club['id'] == '1') echo ( ' SELECTED' );
echo ( '>'.$club['name'].'</option>' );
}
echo ( '
</select>
' );
?>
</li>
<li class="td4"></li>
<li class="td4"><a href="#" onclick="$('ul#team_<?=$loop; ?> li.td4 input[name=wins[]]').val(parseInt($('ul#team_<?
=$loop; ?> li.td4 input[name=wins[]]').val())+1); return false;">+</a> <input type="text" name="wins[]" value="0" /></li>
<li class="td4"><a href="#" onclick="$('ul#team_<?=$loop; ?> li.td4 input[name=draws[]]').val(parseInt($('ul#team_<?
=$loop; ?> li.td4 input[name=draws[]]').val())+1); return false;">+</a> <input type="text" name="draws[]" value="0" /></li>
<li class="td4"><a href="#" onclick="$('ul#team_<?=$loop; ?> li.td4 input[name=losses[]]').val(parseInt
($('ul#team_<?=$loop; ?> li.td4 input[name=losses[]]').val())+1); return false;">+</a> <input type="text" name="losses[]"
value="0" /></li>
<li class="td4"><a href="#" onclick="$('ul#team_<?=$loop; ?> li.td4 input[name=glsfor[]]').val(parseInt
($('ul#team_<?=$loop; ?> li.td4 input[name=glsfor[]]').val())+1); return false;">+</a> <input type="text" name="glsfor[]"
value="0" /></li>
<li class="td4"><a href="#" onclick="$('ul#team_<?=$loop; ?> li.td4 input[name=glsaga[]]').val(parseInt
($('ul#team_<?=$loop; ?> li.td4 input[name=glsaga[]]').val())+1); return false;">+</a> <input type="text" name="glsaga[]"
value="0" /></li>
<li class="td4">- <input type="text" name="pts[]" /></li>
</ul>
<?php
}
} else {
$loop = 0;
while($row = $SITE -> FetchArray($query)) {
++$loop;
?>
<ul class="tr" id="team_<?=$loop; ?>">
<li class="td-squadno"><?=$loop; ?></li>
<li class="td-club" style="width: 160px; margin-left: 10px; background-color: #<?=$row['colour']; ?>; color: #<?
=$row['txtcolour']; ?>;"><?=$row['name']; ?><input type="hidden" name="cid[]" value="<?=$row['cid']; ?>" /></li>
<li class="td4"><?=$row['pld']; ?></li>
<li class="td4"><a href="#" onclick="$('ul#team_<?=$loop; ?> li.td4 input[name=wins[]]').val(parseInt($('ul#team_<?
=$loop; ?> li.td4 input[name=wins[]]').val())+1); return false;">+</a> <input type="text" name="wins[]" value="<?=$row
['wins']; ?>" /></li>
<li class="td4"><a href="#" onclick="$('ul#team_<?=$loop; ?> li.td4 input[name=draws[]]').val(parseInt($('ul#team_<?
=$loop; ?> li.td4 input[name=draws[]]').val())+1); return false;">+</a> <input type="text" name="draws[]" value="<?=$row
['draws']; ?>" /></li>
<li class="td4"><a href="#" onclick="$('ul#team_<?=$loop; ?> li.td4 input[name=losses[]]').val(parseInt
($('ul#team_<?=$loop; ?> li.td4 input[name=losses[]]').val())+1); return false;">+</a> <input type="text" name="losses[]"
value="<?=$row['losses']; ?>" /></li>
<li class="td4"><a href="#" onclick="$('ul#team_<?=$loop; ?> li.td4 input[name=glsfor[]]').val(parseInt
($('ul#team_<?=$loop; ?> li.td4 input[name=glsfor[]]').val())+1); return false;">+</a> <input type="text" name="glsfor[]"
value="<?=$row['glsfor']; ?>" /></li>
<li class="td4"><a href="#" onclick="$('ul#team_<?=$loop; ?> li.td4 input[name=glsaga[]]').val(parseInt
($('ul#team_<?=$loop; ?> li.td4 input[name=glsaga[]]').val())+1); return false;">+</a> <input type="text" name="glsaga[]"
value="<?=$row['glsaga']; ?>" /></li>
<li class="td4"><?=$row['points']; ?> (- <input type="text" name="pts[]" value="<?=$row['pts']; ?>" />)</li>
</ul>
<?php
}
}
?>
<div class="tf"><input type="submit" value="Update Standings" /> or <a href="<?=$CONF['site']; ?>sitecontrol/fixtures/<?
=$url[2].'/'.$url[3]; ?>">Cancel</a></div>
</div>
</form>
<p>Do not use the points column for anything other than points deductions. If a points deduction is carried out by the
Premier League, use the input box to enter how many points have been taken away, e.g. Portsmouth in 2009/10 would have
"9" in their points input box.</p>
<p>Matches played and points columns will update automatically. The position of each team in the league table will also be
automatically updated.</p>
<?php
} else {
?>
<ul class="buttonbar">
<li class="button">Add Fixture</li>
<li class="button">Standings</li>
</ul>
<div class="table">
<div class="th">Premier League Fixtures</div>
<?php
$query = $SITE -> Query ( '
SELECT a.id, a.rid, home, away, homescore, awayscore, date, b.name as hometeam, c.name as awayteam FROM
got_fixtures a
inner join got_clubs b on (a.home = b.id)
inner join got_clubs c on (a.away = c.id)
WHERE a.cid="1" AND a.season = "'.$url[2].'" AND (a.home = "1" OR a.away = "1")
ORDER BY date ASC' );
while($row = $SITE -> FetchArray($query)) {
?>
<ul class="tr">
<li class="td-squadno"><?=$row['rid']; ?></li>
<li class="td-home"><?=$row['hometeam']; ?></li>
<li class="td-score"><?=$row['homescore']; ?> - <?=$row['awayscore']; ?></li>
<li class="td-away"><?=$row['awayteam']; ?></li>
<li class="td-kickoff"><?=date('D. jS F y, H:i', $row['date']); ?></li>
<li class="td-end"><a href="<?=$CONF['site']?>sitecontrol/fixtures/<?=$url[2]; ?>/edit/<?=$url[3]; ?>/<?=$row['id'];
?>">Edit</a> or <a href="<?=$CONF['site']; ?>mcms/applications/fixtures/submit.php?do=fixtures&action=delete&id=<?
=$row['id']; ?>&comp=<?=$url[3]; ?>&season=<?=$url[2]; ?>">Remove</a></li>
</ul>
<?php
}
?>
<div class="tf"></div>
</div>
Sounds like it's not inserting it correctly into the database. Look for a SQL query that has INSERT or UPDATE in it.
The SQL in the code above is displaying data from the database, not adding to it. Your problem will be elsewhere. As the while loop pulling from the database was hard coded with the value 19, then the update code will probably be as well.
Also login to your hosting account, go to PhpMyAdmin, and have a look at the database itself. See how that is set up.

Categories