Echoing an abstract variable in PHP - 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} ?>

Related

Echoing nested JSON into PHP

I have a JSON feed which I am parsing via PHP. I am having issues getting some nested elements to echo which I would appreciate some assistance on.. I've looked at loads of related posts here but cant seem to get the logic to work on my specific JSON feed. Could someone advise what I am doing wrong?
JSON feed is here > https://api.lever.co/v0/postings/leverdemo?skip=1&limit=3&mode=json
The elements, I am struggling to parse are the "categories" parent and child nodes of "team", "location" and "commitment".
I was thinking this would work - but it does not...
<?php
$url = 'feed.json';
$data = file_get_contents($url);
$characters = json_decode($data, true);
?>
<table>
<tbody>
<tr>
<th>Job title</th>
<th>Team</th>
<th>Location</th>
<th>Commitment</th>
<th>DescriptionPlain</th>
<th>applyUrl</th>
</tr>
<?php foreach ($characters as $character) : ?>
<tr>
<td> <?php echo $character['text']; ?> </td>
<td> <?php echo $character['categories'][2]['team'] ?></td>
<td> <?php echo $character['categories'][2]->team ?></td>
<td> <?php echo $character['categories'][1]->location ?></td>
<td> <?php echo $character['categories'][0]->commitment ?></td>
<td> <?php echo $character['descriptionPlain']; ?> </td>
<td> <?php echo $character['applyUrl']; ?> </td>
</tr>
<?php endforeach; ?>
</tbody>
Note, its just the categories children that fail to echo? Also noticed that if I use the full url in the $url variable it all fails? But from local it works??
Any ideas??? Thanks!
It should be:
<td> <?php echo $character['categories']["team"] ?></td>
<td> <?php echo $character['categories']["location"] ?></td>
<td> <?php echo $character['categories']["commitment"] ?></td>
instead. The numeric keys are not present on the array in the data. Also "categories" is not an object, so you cannot use the arrow (->) notation.
EDIT
You get an error because you are trying to access an object, actually you have an array, here is the solution hope it helps :
<?php
$url = 'https://api.lever.co/v0/postings/leverdemo?skip=1&limit=3&mode=json';
$data = file_get_contents($url);
$characters = json_decode($data, true);
$nb = count($characters);
?>
<table>
<tbody>
<tr>
<th>Job title</th>
<th>Team</th>
<th>Location</th>
<th>Commitment</th>
<th>DescriptionPlain</th>
<th>applyUrl</th>
</tr>
<?php while($nb > 0){
$nb--;
$nb_lists = count($characters[$nb]['lists']);
?>
<tr>
<?php
while($nb_lists > 0){
$nb_lists--;
?>
<td> <?php if(isset($characters[$nb]['lists'][$nb_lists]['text'])){ echo $characters[$nb]['lists'][$nb_lists]['text'];} ?> </td>
<?php } ?>
<td> <?php if(isset($characters[$nb]['categories']['team'])){echo $characters[$nb]['categories']['team'];} ?></td>
<td> <?php if(isset($characters[$nb]['categories']['team'])){echo $characters[$nb]['categories']['team'];} ?></td>
<td> <?php if(isset($characters[$nb]['categories']['location'])) {echo $characters[$nb]['categories']['location'];} ?></td>
<td> <?php if(isset($characters[$nb]['categories']['commitment'])){ echo $characters[$nb]['categories']['commitment'];} ?></td>
<td> <?php if(isset($characters[$nb]['descriptionPlain'])){echo $characters[$nb]['descriptionPlain']; }?> </td>
<td> <?php if(isset($characters[$nb]['applyUrl'])){echo $characters[$nb]['applyUrl'];} ?> </td>
</tr>
<?php } ?>
</tbody>

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.

how to store json_encode results in a table

I have a database which works by displaying first and last_names of all employees in the database, i can display it but when it displays, it's not formatted. I want to try and put the results in a table but I'm not sure how I would.
I thought I would have to echo json_encode(echo.<td>$posts</td>) or something like that
<?php foreach($query as $row): ?>
<tr>
<td>
<?php $arr = array(
'first_name' => $row->first_name,
'last_name' => $row->last_name,
); ?>
<?php $posts[] = $arr;?>
</tr>
<?php endforeach; ?>
<?php echo json_encode($posts);?>
This is how its displayed now
[{"first_name":"Georgi","last_name":"Facello"},
{"first_name":"Georgi","last_name":"Atchley"}]
Nothing is written between your <tr> and such .. you are just assigning to posts and then printing it out as JSON after the fact which makes no sense.
<?php foreach... ?>
<tr>
<td>
<?php echo $row->first_name ?>
</td>
<td>
<? php echo $row->last_name ?>
</td>
</tr>
<?php endforeach ?>
You're gonna have to build the table 'by hand'. Like this:
<table>
<?php foreach($query as $row): ?>
<tr>
<td>
<?php echo $row->first_name; ?>
</td>
<td>
<?php echo $row->last_name; ?>
</td>
</tr>
<?php endforeach; ?>
</table>
<table>
<thead>
<th> First Name </th>
<th> Last Name </th>
</thead>
<tbody>
<?php foreach($query as $row): ?>
<tr>
<td> <?php echo $row->first_name ?> </td>
<td> <?php echo $row->last_name ?> </td>
</tr>
<?php endforeach; ?>
</tbody>
</table>

How to hide table if a variable empty

I have table with variables in it. I want to hide whole table if the first variable is empty. I want to hide id="con_industry (whole table) if $list_primary_industry variable is empty. Here is my code:
<table class="admintable" id="con_industry">
<tr id="con_id_industry1">
<td class="key"><?php echo JText::_('PRIMARY_INDUSTRY');
?></td<td>echo $list_primary_industry; ?></td></tr>
</table>
Set a check before your table:
<?php if(isset($list_primary_industry) && !empty($list_primary_industry)): ?>
<table class="admintable" id="con_industry">
<tr id="con_id_industry1">
<td class="key"><?php echo JText::_('PRIMARY_INDUSTRY'); ?></td>
<td>echo $list_primary_industry; ?></td>
</tr>
</table>
<?php endif; ?>
Like this: (fixed your code a bit)
<? if($list_primary_industry) {?>
<table class="admintable" id="con_industry">
<tr id="con_id_industry1">
<td class="key"><?php echo JText::_('PRIMARY_INDUSTRY');
?></td><td><? echo $list_primary_industry; ?></td></tr>
</table>
<?}?>

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

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

Categories