Codeigniter issue with loop and Condition - php

I am loading a view like this
if($type == 'view'){
// do something
}else if($type == 'insert'){
// Here i am making a form
?>
<select>
<?php
foreach($applications as $row)
{
?>
<option value = "<?php echo $row->id;?>">
<?php echo $row->name;?>
</option>
<?php } ?>
</select>
<?
}else{
//do some thing else
}
Now this is the error i am encountering:
Parse error: syntax error, unexpected T_ELSE in url on line xx
The weird thing is that if i comment out the loop if works fine. What is the problem and how can it be resolved?

you need to use codeigniter loops in view way like this:
<?php foreach ($applications as $row):?>
<option value = "<?php echo $row->id; ?>">
<?php echo $row->name; ?>
</option>
<?php endforeach;?>

It works fine for me... try again
<?php
if($type == 'view'){
// do something
}else if($type == 'insert'){
// Here i am making a form
?>
<select>
<?
foreach($applications as $row){
?><option value = "<?php echo $row->id;?>"><?php echo $row->name;?></option>
<?php
}
?>
</select>
<?
}else{
//do some thing else
}

Using switch, you have a more elegant way of code writing and of course faster, try:
switch($type){
case 'view':
//do something
break;
case 'insert':
echo '<select>';
foreach($applications as $row){
echo '<option value="'.$row->id.'">'.$row->name.'</option>';
}
echo '</select>';
break;
default:
//do something else
break;
}

Maybe your want like this code:
<?php
if ($type == 'view')
{
// do something
}
elseif ($type == 'insert')
{
// Here i am making a form
?>
<select>
<?php
foreach($applications as $row)
{
?>
<option value = "<?php echo $row->id;?>"><?php echo $row->name;?></option>
<?php
}
?>
</select>
<?php
}
else
{
//do some thing else
}
?>

Related

How to echo PHP and HTML in if statement

I have an if statement that only has to show some code if the value of $result37 = ncrteam... but how can I echo that HTML and PHP code? echo" "; is not working and echo ' '; Is also not working.
This is my code:
<?php
if ($result37 === "ncrteam") {
?>
Status:<br>
<select class="form-control" name="status" style="width: 300px">
<?php
while ($row15 = mysqli_fetch_assoc($result15)):; ?>
<option selected value=\"<?php echo $row15['status'];?>\"><?php echo $row15['status'];?></option>
<?php endwhile;?>
<?php while ($row16 = mysqli_fetch_assoc($result16)):; ?>
<option value=\"<?php echo $row16['statusname'];?>\"><?php echo $row16['statusname'];?></option>
<?php endwhile;?>
</select>
<?php
}
?>
This is not a complete answer, because it is unclear what exactly you are trying to do, but your question looks something like How do I build html based on values of my php variables and I will try to answer it as best I can
With the limited information I have from your question, I think this is the kind of thing you are trying to do:
<?php
...
$html = "";
if ($result37 === "ncrteam") {
while ($row15 = mysqli_fetch_assoc($result15))
{
$html .= "<option value=".$row15['status'].">";
}
}
?>
<html>
<body>
...
<select>
<?php echo $html; ?>
</select>
...
</body>
</html>

Drop Down list in php (help)

Well, this is my first question in stackoverflow. I hope that you can help me!
I would like to make a select box or a drop down list, if you prefer
So, i have this php code:
<?php $i = 0; ?>
<?php $orders = osc_list_orders();
foreach($orders as $label => $params) {
$orderType = ($params['iOrderType'] == 'asc') ? '0' : '1'; ?>
<?php if(osc_search_order() == $params['sOrder'] && osc_search_order_type() == $orderType) { ?>
<a class="current" href="<?php echo osc_update_search_url($params); ?>"><?php echo $label; ?></a>
<?php } else { ?>
<?php echo $label; ?>
<?php } ?>
<?php if ($i != count($orders)-1) { ?>
<span>|</span>
<?php } ?>
<?php $i++; ?>
<?php } ?>
I have tried to do something like this:
function osc_list_orders_drop() {
$orders = osc_list_orders();
echo '<select name="order" id="order" ONCHANGE="location = this.options[this.selectedIndex].value;">';
foreach($orders as $label => $params){
$orderType = ($params['iOrderType'] == 'asc') ? '0' : '1';
echo '<option value="'.osc_update_search_url($params).'">';
echo $label;
echo'</option>';
}
echo'</select>';
}
But the first option is not stored, so when i select one option and then try to select the first one, the first one never works.
Can you help me?
Thx
Try adding a blank option tag in the select, it will allow the change to trigger.
function osc_list_orders_drop() {
$orders = osc_list_orders();
echo '<select name="order" id="order" ONCHANGE="location = this.options[this.selectedIndex].value;">';
echo '<option></option>';
foreach($orders as $label => $params){
$orderType = ($params['iOrderType'] == 'asc') ? '0' : '1';
echo '<option value="'.osc_update_search_url($params).'">';
echo $label;
echo'</option>';
}
echo'</select>';
}
A better option would be to add a selected="selected" to the current filter's option tag.

PHP Select Box...after selection help needed (yii)

I am using a script that runs on the YII framework...
I have this select box that lets a user view content based on their country. But right now, when a user selects their country, after the selection, it just shows the country name...
And what I want is, after a user selected their country... it must still show the select box, but have the selected country at the top of the list.
I have tried so many things but I'm still new to this this.
<?php if ( $_country == '' ) { ?>
<select onchange="changeCountry(this)" name="country" id="country">
<?php if ( count ($c) != 0 ) { ?>
<?php if ( $_country == NULL ) ?>
<?php foreach ($c as $id => $val) { ?>
<option<?php if ($id == $countryId) {?> selected="selected"<?php } ?> value="<?php echo mbCoreFunctions::to_seo_ad_name ($val, $id) ?>"><?php echo $val; ?></option>
<?php } ?>
<?php } ?>
</select>
<?php } else { ?>
<span class="countryName"><?php echo $countryName; ?></span>
<?php } ?>
I didn't try but following should work
<select onchange="changeCountry(this)" name="country" id="country">
<?php foreach ($c as $id => $val) { ?>
<option<?php if ($id == $countryId) {?> selected="selected"<?php } ?> value="<?php echo mbCoreFunctions::to_seo_ad_name ($val, $id) ?>"><?php echo $val; ?></option>
<?php } ?>
</select>

Change currency icon block to drop-down list

My client would prefer a currency drop-down list to the currency icon block installed on a 1.5.1 OpenCart theme. I've tried coding it but get the following error:
Parse error: syntax error, unexpected T_STRING, expecting ';' in /...file path......
I've pasted the code and used before and after the offending line.
<?php
$a = 0;
foreach ($currencies as $currency) {
$thisCurTitle[$a] = $currency['title'];
$thisCurCode[$a] = $currency['code'];
if ($currency['symbol_left']) {
$thisCurSymb[$a] = $currency['symbol_left'];
} else {
$thisCurSymb[$a] = $currency['symbol_right'];
}
$a++;
}
?>
<select name=”curselect” onchange=”$(‘input[name=\'currency_code\']‘).attr(‘value’, this.options[this.selectedIndex].value).submit(); $(this).parent().parent().submit();”>
***<?php for ($z = 0; $z <= $a – 1; $z++) { ?>***
<?php if ($thisCurCode[$z] == $currency_code) { ?>
<option value=”<?php echo $thisCurCode[$z]; ?>” selected><?php echo $thisCurTitle[$z]; ?> <?php echo $thisCurSymb[$z]; ?></option>
<?php } else { ?>
<option value=”<?php echo $thisCurCode[$z]; ?>”><?php echo $thisCurTitle[$z]; ?> <?php echo $thisCurSymb[$z]; ?></option>
<?php } ?>
<?php } ?>
Any help would be most appreciated.
$a – 1 contains something like an m-dash, or whatever it's called. Not a - minus sign. This most likely happened because you copy-pasted code that had been through an auto-formatter like some mail programs or word processors.
after 2 hours of work here is my solution
<form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" name="formcur" id="formcur">
<div id="currency">
<?php echo $text_currency; ?>
<?php
$a = 0;
foreach ($currencies as $currency) {
$thisCurTitle[$a] = $currency['title'];
$thisCurCode[$a] = $currency['code'];
if ($currency['symbol_left']) {
$thisCurSymb[$a] = $currency['symbol_left'];
} else {
$thisCurSymb[$a] = $currency['symbol_right'];
}
$a++;
}
?>
<select name="curselect" id="curselect" onchange="$('input[name=\'currency_code\']').attr('value', document.getElementById('curselect').value); document.forms['formcur'].submit();">
<?php
for ($z = 0; $z <= $a - 1; $z++) {
if ($thisCurCode[$z] == $currency_code) { ?>
<option value="<?php echo $thisCurCode[$z]; ?>" selected><?php echo $thisCurTitle[$z]; ?> <?php echo $thisCurSymb[$z]; ?></option>
<?php
} else {
?>
<option value="<?php echo $thisCurCode[$z]; ?>"><?php echo $thisCurTitle[$z]; ?> <?php echo $thisCurSymb[$z]; ?></option>
<?php } ?>
<?php } ?>
</select>
<input type="hidden" name="currency_code" value="" />
<input type="hidden" name="redirect" value="<?php echo $redirect; ?>" />
</div>
</form>
I think your problem is that the value returned in your if block is a String?
if ($currency['symbol_left']) {
Had to clean up your code to be able to make any sense of it. You had a load of odd ` chars stick to " and ' and try to keep inside PHP as much as possible.... this is not an answer yet.... I suggest you upload this and identify to us exaclty which line is at fault, I suspect its in the first echo
<?php
$a = 0;
foreach ($currencies as $currency)
{
$thisCurTitle[$a] = $currency['title'];
$thisCurCode[$a] = $currency['code'];
if ($currency['symbol_left'])
{
$thisCurSymb[$a] = $currency['symbol_left'];
}
else
{
$thisCurSymb[$a] = $currency['symbol_right'];
}
$a++;
}
echo "<select name='curselect' onchange='$('input[name=\'currency_code\']').attr('value’, this.options[this.selectedIndex].value).submit(); $(this).parent().parent().submit();'>";
for ($z = 0; $z <= $a – 1; $z++)
{
if ($thisCurCode[$z] == $currency_code)
{
echo "<option value='".$thisCurCode[$z]."' selected>".$thisCurTitle[$z]." ".$thisCurSymb[$z]."</option>";
}
else
{
echo "<option value='".$thisCurCode[$z]."'>".$thisCurTitle[$z]." & nbsp;".$thisCurSymb[$z]."</option>";
}
}
?>
found the problem.....
You have placed a calculation inside the for() statement...
$a =10; // this is to mimic your counter
$a=$a-1; // do you subtract here
//for ($z = 0; $z <= $a – 1; $z++) // this is the issue.
//for ($z = 0; $z <= ($a – 1); $z++) // doesn't work either.
for($z=0; $z <= $a; $z++)
{
echo $z." hello</br />";
}
http://php.net/manual/en/control-structures.for.php
cant see any reason why you wouldnt be able to do it the way you tried, my PHP errors in the same way, even encapsulating the subtract in ()... fixes the issue but still bothers me. Rarely use for()...
I hope that fixes your trouble!

How to properly use an if-statement with the <option> tag?

So what I have is an array of things. The items in the array are put into a drop down with the tag, nothing new or complicated about that but here's the (current working) code anyway:
<?php foreach($roles as $role): ?>
<option value="<?php echo $role['id']; ?>"><?php echo $role['name']; ?></option>
<?php endforeach ?>
Now what needs to happen, is that if the $role['name'] is "Basic", it should not be displayed. I have been trying (and googling) for this, and I have been unsuccessful. I didn't think it was that big of a deal.
Here's what I'm trying:
<?php foreach($roles as $role): ?>
<?php if(!$role['name'] == "Basic") { ?>
<option value="<?php echo $role['id']; ?>"><?php echo $role['name']; ?></option>
<?php } ?>
<?php endforeach ?>
When I try that it doesn't add any fields to the drop down at all, so I'm obviously missing something here. Any tips would be appreciated, thanks!
Your problem is with operator precedence [docs]:
!role['name'] == "Basic"
Assuming that role['name'] is never empty or contains the string "0" (see converting to boolean), this will be evaluated as
false == "Basic"
which is always false. Use != instead or write !(role['name'] == "Basic").
You can use the alternative style for the if statement too:
<?php foreach($roles as $role): ?>
<?php if($role['name'] != "Basic"): ?>
...
<?php endif ?>
<?php endforeach ?>
<?php
foreach($roles as $role)
{
if($role['name'] != "Basic")
{
echo '<option value="'.$role['id'].'">'.$role['name'].'</option>';
}
}
?>
use
<?php if($role['name'] != "Basic") { ?>
instead of
<?php if(!$role['name'] == "Basic") { ?>
Your if() is incorrect:
if(!$role['name'] == "Basic")
What you intended was probably this:
if($role['name'] != "Basic")

Categories