I can't get div id displayed in loop in Code Igniter - php

I have a command that creates a loop:
foreach($cart as $line=>$item).
When it displays, it shows the HTML quoted below. I have tried to add a div id below the loop, as I want to create a getElementById() function. However, it creates a loop above on its own. Also, I cannot change the tr statement. I have actually done a search and replace on every tr statement in Code Igniter and it is still there. I am quite a novice, and would really appreciate any advice, as this has been baffling me for three days now.
<tbody id="cart_contents">
<?php
if(count($cart)==0)
{
?>
<tir><td colspan='8'>
<div class='warning_message' style='padding:7px;'><?php echo $this->lang->line('sales_no_items_in_cart'); ?></div>
</tr></tr>
<?php
}
else
{
echo "</tr><tir>";
foreach($cart as $line=>$item)
{
?>
<td id = " <?php echo $item['name'];?>" style="align:center;" ><?php echo $item['name']; ?></td>
<?php if ($items_module_allowed)
{
?>
<td><?php echo form_input(array('name'=>'price','value'=>$item['price'],'size'=>'6'));?></td>
<?php
}
else
{
?>
<td><?php echo $item['price']; ?></td>
<?php echo form_hidden('price',$item['price']); ?>
<?php
}
?>
<td>
<?php
if($item['is_serialized']==1)
{
echo $item['quantity'];
echo form_hidden('quantity',$item['quantity']);
}
else
{
echo form_input(array('name'=>'quantity','value'=>$item['quantity'],'size'=>'2'));
}
?>
</td>
<td><?php echo to_currency($item['price']*$item['quantity']-$item['price']*$item['quantity']*$item['discount']/100); ?></td>
<?php
if($item['allow_alt_description']==1)
{
}
else
{
if ($item['description']!='')
{
}
else
{
}
}
?>
</td>
<td> </td>
<td style="color:#2F4F4F";>
<?php
if($item['is_serialized']==1)
{
}
?>
</td>
<td colspan=3 style="text-align:left;">
<?php
if($item['is_serialized']==1)
{
}
?>
</td>
</tr>
<tr style="height:3px">
<td colspan=8 style="background-color:white"> </td>
</tr> </form>
<?php
}
}
?>
</tbody>
</table> </div>
This is the HTML output from the original loop.
<tr><td id=" test" style="align:center;">test</td>
<td><input type="text" name="price" value="150.00" size="6"></td>
<td>
etc...
</td>
</tr>

I don't believe your problem is with your php, I think it is your html.
Your html opening and closing tags do not all match up. You have several unclosed <tr> elements, at least one un-opened <tr>, your form tags don't work out given your if/else statement structure, and you don't actually have an opening <table> tag or an opening <div> tag at the top of your code while you have both at the end. The div that you are getting above your loop that you expect to occur IN your loop is probably the result of your browser trying to interpret the broken page structure.
As far as the form tags I mentioned, make sure that if you use an if statement to open a form, you must also have a condition that will open any required forms if the if statement evaluates to false. I think you have a form element inside an if, then form inputs as part of the next else statement.
I've simplified your posted code and included comments to try and illustrate this:
<tbody id="cart_contents">
<?php if(){?>
<tir><!--what's a 'tir'?, did you mean 'tr'?-->
<td colspan='8'>
<div class='warning_message' style='padding:7px;'>
</div>
</tr>
</tr>
<?php }else{
echo "</tr><tir>";//here you are closing a row that was never opened, and another 'tir'.
foreach($cart as $line=>$item){?>
<td></td>
<?php if (){ //here you call a form input before the form is created...?>
<td><?php echo form_input();?></td>
<?php }else{?>
<td></td>
<?php echo form_hidden();
//this form isn't in a table element, which might act weird?>
<?php }?>
<td><?php if() {
echo form_hidden();
}else{
echo form_input());
//here you have a form input outside of a form if
//the previous if statement evaluates to false.
}?>
</td>
<td></td>
<?php if(){ }
else {
if(){}
else{}
}?>
</td><!--this td was never opened-->
<td> </td>
<td></td>
<td></td>
</tr><!--this tr was never opened-->
<tr><td></td></tr>
</form><!--I don't believe you can count on your conditional statements to garantee that a form was opened-->
<?php }
} ?>
</tbody>
</table> <!--you haven't opened your table in this code-->
</div><!--you havent' opened a div in this code-->

Related

Echoing an abstract variable in PHP

So I have some code that looks like this
<tr>
<td>
<?php echo "text_". $textnum ?>
</td>
</tr>
And I want the code to function the same as
<tr>
<td>
<?php echo $text_x ?>
</td>
</tr>
Where X is the value of $textnum. I cannot just use <?php echo $text_1 ?> because I dont know which text variable i am going to be echoing
Use <?=${'text_'.$textnum};?> or <?php echo ${'text_'.$textnum} ?>

get unique ID's of rows in php foreach loop for JQuery

Good Afternoon,
I have 3 foreach loops on my page, the first gets the teams, the second gets each person in the team, and the third gets each unique reference to a task that the agent has completed. I have collected this data and it is being displayed fine. I now want to add some JQuery to it so it will hide the agents and references unless the relevant team or agent is clicked on.
So if I load the page everything will be hidden apart from the teams, when I click on a team it will show the agents, when I click on an agent it will show the references.
I am having trouble assigning unique ID's to each row and finding those in the JQuery script.
Here is my code...
<?php if($aForm['sTaskType'] !== 'CP' ){?>
<table style="width: 95%">
<tr>
<th>Area</th>
<th>Pass</th>
<th>Pass with feedback</th>
<th>Fail with Minors</th>
<th>Fail with Majors</th>
</tr>
<?php foreach ($aQualityTeamResults AS $iBusinessStreamId => $aTeamData) {
$aQualityAgentResults = $oRadiusQualityFns->GetQualityAgentResults($sDateFrom, $sDateTo, $sTaskType, $aTeamData['iBusinessStreamId']);?>
<tbody>
<tr class="TeamClick<?php echo $aTeamData['iBusinessStreamId'];?>">
<td><?php echo $aTeamData['sBusinessStream']?></td>
<td><?php echo $aTeamData['Pass']?></td>
<td><?php echo $aTeamData['Pass with Feedback']?></td>
<td><?php echo $aTeamData['Fail with Minors']?></td>
<td><?php echo $aTeamData['Fail with Majors']?></td>
</tr>
</tbody>
<?php foreach ($aQualityAgentResults AS $iUserId => $aAgentData) {
$aQualityPropertyResults = $oRadiusQualityFns->GetQualityPropertyResults($sDateFrom, $sDateTo, $sTaskType, $aAgentData['iBusinessStreamId'], $aAgentData['Agent']);
?>
<tbody>
<tr class="Agent<?php echo $iUserId]?>">
<td><?php echo $oRadiusUser->Get_User_Name($aAgentData['Agent']);?></td>
<td><?php echo $aAgentData['Pass'];?></td>
<td><?php echo $aAgentData['Pass with Feedback'];?></td>
<td><?php echo $aAgentData['Fail with Minors'];?></td>
<td><?php echo $aAgentData['Fail with Majors'];?></td>
</tr>
</tbody>
<?php foreach ($aQualityPropertyResults AS $iUserId => $aPropertyData) { ?>
<tbody>
<tr class="Property<?php echo $aPropertyData['iUserId'];?>">
<td colspan="2"><font color="black"><?php echo $aPropertyData['sPropertyCode']?></font></td>
<td colspan="3"><?php echo $aPropertyData['Result']?></td>
</tr>
</tbody>
<?php
}
}
}
?>
</table>
I have given each of the rows a unique class by adding in the unique identifier from the database. I just dont know how to find these within the Jquery script.
EDIT:
Maybe not explained myself properly, I would like help with how to set up this script but obviously a lot better.
<script language="javascript" type="text/javascript" >
$(document).ready(function() {
$('.Agent').hide;
$('.Property').hide;
$(document).on('click','.TeamClick',function(){
$('.Agent').toggle('show');
$('.Property').toggle('show');
});
});
</script>
But in this case it will show/hide all of the rows as they will all have the same id's, whereas now I have added on the unique id on the end with the php code in the class, I dont know how to call those as they all are called different classes.
So if I click on TeamClick1, it shows the actual rows for that team (Agent1), and not all of them. but obviously I cant type out all of the unique id's I just dont know how to get them from the php in JQuery.
<script language="javascript" type="text/javascript" >
$(document).ready(function() {
$('.Agent').hide;
$('.Property').hide;
$(document).on('click','.TeamClick(UNIQUE ID)',function(){
$('.Agent(UNIQUE ID)').toggle('show');
$('.Property(UNIQUE ID)').toggle('show');
});
});
Hope this makes sense.
I am hasty but it is like this
<?php if($aForm['sTaskType'] !== 'CP' ){?>
<table style="width: 95%">
<tr>
<th>Area</th>
<th>Pass</th>
<th>Pass with feedback</th>
<th>Fail with Minors</th>
<th>Fail with Majors</th>
</tr>
<?php foreach ($aQualityTeamResults AS $iBusinessStreamId => $aTeamData) {
$aQualityAgentResults = $oRadiusQualityFns->GetQualityAgentResults($sDateFrom, $sDateTo, $sTaskType, $aTeamData['iBusinessStreamId']);?>
<tbody>
<tr class="TeamClick<?php echo $iBusinessStreamId;?>">
<td><?php echo $aTeamData['sBusinessStream']?></td>
<td><?php echo $aTeamData['Pass']?></td>
<td><?php echo $aTeamData['Pass with Feedback']?></td>
<td><?php echo $aTeamData['Fail with Minors']?></td>
<td><?php echo $aTeamData['Fail with Majors']?></td>
</tr>
</tbody>
<?php foreach ($aQualityAgentResults AS $iUserId => $aAgentData) {
$aQualityPropertyResults = $oRadiusQualityFns->GetQualityPropertyResults($sDateFrom, $sDateTo, $sTaskType, $aAgentData['iBusinessStreamId'], $aAgentData['Agent']);
?>
<tbody>
<tr class="Agent<?php echo $iUserId; ?>">
<td><?php echo $oRadiusUser->Get_User_Name($aAgentData['Agent']);?></td>
<td><?php echo $aAgentData['Pass'];?></td>
<td><?php echo $aAgentData['Pass with Feedback'];?></td>
<td><?php echo $aAgentData['Fail with Minors'];?></td>
<td><?php echo $aAgentData['Fail with Majors'];?></td>
</tr>
</tbody>
<?php foreach ($aQualityPropertyResults AS $iUserId2 => $aPropertyData) { ?>
<tbody>
<tr class="Property<?php echo $iUserId2 ;?>">
<td colspan="2"><font color="black"><?php echo $aPropertyData['sPropertyCode']?></font></td>
<td colspan="3"><?php echo $aPropertyData['Result']?></td>
</tr>
</tbody>
<?php
}
}
}
?>
</table>
I'm putting the identifiers in classes, but if you want put in IDs you can, is the same thing, like this id="<?php ... ?>"

php joomla k2 code suggestion

i'm working with k2, the joomla content module.
i'm using extra fields and i have a particular need.
i associated some link type extra fields to a k2 category:
i need them to be invisible to users while they create items from frontend,
but then come back visible after i (administrator) filled those fields from backend.
so i just need to hide those extrafields (all the link type) from the itemform view: below is the code where i should add something like
IF
THEN
ELSE
END
but i don't know nothing about php code compiling... can any body suggest something???
CODE:
<table class="admintable" id="extraFields">
<?php foreach($this->extraFields as $extraField): ?>
<tr>
<td align="right" class="key">
<?php echo $extraField->name; ?>
</td>
<td>
<?php echo $extraField->element; ?>
</td>
</tr>
<?php endforeach; ?>
</table>
Change that code to this -
<table class="admintable" id="extraFields">
<?php foreach($this->extraFields as $extraField): ?>
<?php if ($extrafield->name !="name of field you want to hide") { ?>
<tr>
<td align="right" class="key">
<?php echo $extraField->name; ?>
</td>
<td>
<?php echo $extraField->element; ?>
</td>
</tr>
<?php } ?>
<?php endforeach; ?>
</table>
Be sure to do it as a template override so it doesn't get killed when you update.

Table row beside each other

I tried to allow the last td in this code to be beside the previous td, but I can't and the td is printed in a new line. How to allow them to be beside each other,the problem is that the 1st 5 td are in a foreach loop and the last td is not follow this foreach as it's value is a function and not a key or a value in the foreach.
<?php foreach($downloads as $dl) { ?>
<tr id="this">
<td ><img src="images/<?=$dl['type']?>.png"/></td>
<td id="no3"><?=$dl['type']?></td>
<td>
<a target="_blank" style="margin-right:3px" href="download.php?id=<?=$dl['id']?>">
<?=$dl['title']?>
</a>
</td>
<td>
<center>
<?=$dl['sname']?>
</center>
</td>
<td align="center"><?=$dl['views']?></td>
</tr>
<?php } ?>
<td align="center"><?=$core->use_love(); ?></td>
The function of the last td
public function use_love(){
$sql=mysql_query("select * from wcddl_downloads ORDER BY id DESC LIMIT ".$this->pg.",".$this->limit."");
while($row=mysql_fetch_array($sql))
{
$down_id=$row['id'];
$love=$row['love'];
?>
<div class="box" align="center">
<a href="#" class="love" id="<?php echo $down_id; ?>">
<span class="on_img" align="left"> <?php echo $love; ?> </span>
</a>
</div>
<?
}
}
The last <td> (the one outside the foreach loop) is on a new line because it's outside the last <tr> tag. One way to solve this is to always close the </tr> tag after the last <td>, like this:
<?php
$first_time = True;
foreach($downloads as $dl) {
// If this is the first time through the loop, don't echo a </tr> tag:
if ($first_time) {
$first_time = False;
} else {
echo "</tr>";
}
// Now print the new row, but don't close it yet:
?>
<tr id="this">
<td><img src="images/<?=$dl['type']?>.png"/></td>
<td id="no3"><?=$dl['type']?></td>
<td><a target="_blank" style="margin-right:3px" href="download.php?id=<?=$dl['id']?>"><?=$dl['title']?></a></td>
<td><center><?=$dl['sname']?></center></td>
<td align="center"><?=$dl['views']?></td>
<?php
}
?>
<td align="center"><?=$core->use_love(); ?></td>
</tr>
This will always put the last <td> in the final row.
UPDATE: I just saw you added a diagram. This answer now makes no sense as the text description given earlier has nothing to do with what you want to achieve in the diagram.
I submit the modification to Steve Nay's answer as you need the same number of TD in all your TR. When you don't have the same count, you need to use colspan to achieve it. I've added a counter to check if it's the last time you loop. Here it goes:
<?php
$downloads_count = count($downloads);
$counter = 0;
foreach($downloads as $dl) :
$counter++;
// If this is the first time through the loop, don't echo a </tr> tag:
if ($counter > 1) {
echo "</tr>";
}
// Now print the new row, but don't close it yet:
?>
<tr id="this">
<td><img src="images/<?=$dl['type']?>.png"/></td>
<td id="no3"><?=$dl['type']?></td>
<td><a target="_blank" style="margin-right:3px" href="download.php?id=<?=$dl['id']?>"><?=$dl['title']?></a></td>
<td><center><?=$dl['sname']?></center></td>
<td align="center"<?php if ($downloads_count != $counter) echo ' colspan="2"'; ?>><?=$dl['views']?></td>
<?php endforeach; ?>
<td align="center"><?=$core->use_love(); ?></td>
</tr>

Ill formatted HTML from a PHP loop

I am looping through an array and building tables. The HTML is then sent to DOMPDF. However, DOMPDF will not create the PDF if the HTML is ill formatted. I assume that is what is happening in my case. Here is my loop:
<?php foreach($credits as $credit) : ?>
<?php if($credit['credit_type'] == "short") : ?>
<table width="100%" cellpadding="0" cellspacing="0" border="0" style="margin:0px 0px 15px 0px;">
<tr>
<td><strong><?php echo $credit['category_title']; ?></strong></td>
</tr>
<tr>
<td><?php echo $credit['credit_heading']; ?></td>
</tr>
</table>
<?php endif; ?>
<?php if($credit['credit_type'] == "long") : ?>
<?php if($credit['category_title'] != $oldvalue) : ?>
<table width="100%" cellpadding="0" cellspacing="0" border="0" style="margin:0px 0px 15px 0px;">
<tbody>
<?php endif; ?>
<tr>
<?php if($credit['category_title'] != $oldvalue) : ?>
<td width="25%"><strong><?php echo trim($credit['category_title']); ?></strong></td>
<td width="25%"><strong>Title</strong></td>
<td width="25%"><strong>Role</strong></td>
<td width="25%"><strong>Director</strong></td>
<?php endif; ?>
</tr>
<tr>
<td width="25%"><?php echo $credit['credit_heading'];?></td>
<td width="25%"><?php echo $credit['credit_title']; ?></td>
<td width="25%"><?php echo $credit['credit_role']; ?></td>
<td width="25%"><?php echo $credit['credit_director']; ?></td>
</tr>
<?php if($credit['category_title'] != $oldvalue) : ?>
</tbody>
</table>
<?php endif; ?>
<?php $oldvalue = $credit['category_title']; ?>
<?php endif; ?>
<?php endforeach; ?>
I cannot for the life of me work out which tag I am not closing. If anyone could give some insight, that would be fab!
Specifically, the loop is creating rows that show some headings, and then spit out futher rows whenever the category title changes.
This may be a simple solution but perhaps not the best:
I recommend you to use PHP's Tidy class (eventually you'll have to install it first...)
Here is the link for the Tidy class Manual.
At the first line:
ob_start();
This command buffers everything what is outputed by your follwing script.
The code below should be added at the end of your file, or there where you want to show the output.
It first gets the buffer with ob_get_contents() and than it cleans the code up.
Note that you'll eventually have to change the configuration parameters for your needs, there are really very much.
$raw_output = ob_get_clean();
$config = array('indent' => true, 'output-xhtml' => true, 'wrap' => 0);
$tidy = new Tidy;
$tidy->parseString($raw_output, $config, 'utf8');
$tidy->cleanRepair();
echo $tidy;
This Example Code was modified by the original version of the example on php.net.
Hope that helps you.
It's a bit difficult to parse without known more about your data. For example, why is a table for "short" credit open and closed with the record, but the table for "long" credit is conditional on the previous record? Is it because you have a flat data structure so related data shows up as a series of consecutive rows? If that's the case things would be easier if the data were a bit more normalized. I.e. you could iterate through each credit record then through the details separately. Any possibility of fixing your data structure?
Analyzing the code you have, your problem appears to be in the logic for the second section of the code. You are setting the value of the variable $oldvalue at the end of the loop. This is after the logic that closes the table. So if you parse two records that have the same category title the second record will output it's table rows completely outside a table (never mind that it will also have a completely empty row). Additionally, if you have a short credit type following a long the table will never be closed.
That being said, working with what you have I'm guessing you may need something like the following:
// build a dummy "previous" record for the first iteration so the conditionals don't break.
<?php $previous_credit = array('credit_type'=>null,'category'=>null); ?>
<?php foreach($credits as $credit) : ?>
<?php if($credit['credit_type'] == "short" || ($previous_credit['credit_type'] == "long" && $previous_credit['category'] != $credit['category'])) : ?>
</tbody>
</table>
<?php endif; ?>
<?php if($credit['credit_type'] == "short") : ?>
<table width="100%" cellpadding="0" cellspacing="0" border="0" style="margin:0px 0px 15px 0px;">
<tr>
<td><strong><?php echo $credit['category_title']; ?></strong></td>
</tr>
<tr>
<td><?php echo $credit['credit_heading']; ?></td>
</tr>
</table>
<?php endif; ?>
<?php if($credit['credit_type'] == "long") : ?>
<?php if($credit['category_title'] != $previous_credit['category_title']) : ?>
<table width="100%" cellpadding="0" cellspacing="0" border="0" style="margin:0px 0px 15px 0px;">
<tbody>
<tr>
<td width="25%"><strong><?php echo trim($credit['category_title']); ?></strong></td>
<td width="25%"><strong>Title</strong></td>
<td width="25%"><strong>Role</strong></td>
<td width="25%"><strong>Director</strong></td>
</tr>
<?php endif; ?>
<tr>
<td width="25%"><?php echo $credit['credit_heading'];?></td>
<td width="25%"><?php echo $credit['credit_title']; ?></td>
<td width="25%"><?php echo $credit['credit_role']; ?></td>
<td width="25%"><?php echo $credit['credit_director']; ?></td>
</tr>
<?php endif; ?>
<?php $previous_credit = $credit; ?>
<?php endforeach; ?>
<!-- one last table close for the last record -->
</tbody></table>
(That's some ugly code and I don't have time to keep revising it, so ... community wiki in case anyone else wants to clean it up.)

Categories