Html is still visible in CodeIgniter using if else - php

I'm here trying to make if else condition to hiding the html part if $addons_facts var is empty. I am making the addon visible on the food item if the $addons_facts var is not empty.
<?php $addon_data = $this->addons_model->get_addon_by_menu_id($menu_details['id']);
$addons_facts = json_decode($addon_data->addon_fact, true);
?>
<?php if(!empty($addons_facts) && $addons_facts !=='') : ?>
<div class="form-group">
<div class="row">
<div class="col-md-12">
<?php
foreach ($addons_facts as $key => $addon_fact) : ?>
<div class="row">
<div class="col-md-1">
<div class="checkbox">
<label for="check">
<input type="checkbox" id="check" />
<span class="fake-input"></span>
</label>
</div>
</div>
<div class="col-md-7 text-left">
<?php echo sanitize($key); ?>
</div>
<div class="col-md-3">
<?php echo currency(sanitize($addon_fact));?>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
</div>
<?php endif; ?>
When data is present in $addons_facts
When data is absent $addons_facts
As you can clearly see that when data is not present and the html is still visible (checkbox or $). I already tried if else both but unable to figure it out what is going wrong and why if condition not hiding the whole content within if{}.

I figure it out the solution by this: <?php if(!empty($key)) : ?>
<div class="form-group">
<div class="row">
<div class="col-md-12">
<?php
foreach ($addons_facts as $key => $addon_fact) : ?>
<?php if(!empty($key)) : ?>
<?php $randomNum = substr(str_shuffle("0123456789abcdefghijklmnopqrstvwxyzABCDEFGHIJKLMNOPQRSTVWXYZ"), 0, 10); ?>
<div class="row">
<div class="col-1">
<div class="checkbox">
<label for="<?php echo $randomNum; ?>">
<input type="checkbox" id="<?php echo $randomNum; ?>" />
<span class="fake-input"></span>
</label>
</div>
</div>
<div class="col-7 text-left">
<?php echo sanitize($key); ?>
</div>
<div class="col-3">
<?php echo sanitize($addon_fact) ? currency(sanitize($addon_fact)) :'';?>
</div>
</div>
<?php endif; ?>
<?php endforeach; ?>
</div>
</div>
</div>

Related

I have an error in my code that does not allow me to show the results and/or show an error message in php

I have a little problem that might be syntax (since I'm not that good at php). Basically, I'm de-wrapping a code where there will be records where there will be a date (in this case, a foundation date, for example; 20-08-2027). So I created an "if". If there are records with the date, for example, 2027, the records appear. If there is not, then an error message will be displayed saying that there is still no record made with that date. I've tried a few things, but nothing worked. At this point, an error appears. Below will be the error. Please try to help me. It's no use saying more technical things, because I'm not that good at php. Thank you.
Error: "Parse error: syntax error, unexpected 'else' (T_ELSE) in /home/host/public_html/template/year/2027.php on line 300"
2027.php
<section class="content products">
<div class="container">
<h1 class="hidden">Eventos</h1>
<div class="row">
<div class="col-sm-10">
<div class="row grid" id="products">
<div class="releated-products">
<div class="row grid" id="products">
<?php
$sqli=sprintf("SELECT * FROM eventos WHERE YEAR(data) = 2027");
$resu=mysqli_query($con,$sqli);
mysqli_set_charset($con, 'UTF8');
if (mysqli_num_rows($resu)>0) {
while($regi=mysqli_fetch_array($resu)){
$sqli_consulta=sprintf("select * from eventos where id=%d;",$regi['id']);
$resu_consulta=mysqli_query($con,$sqli_consulta);
$regi_consulta=mysqli_fetch_array($resu_consulta);
$linkk='../eventoindividual.php?id='.$regi_consulta['id'];
?>
<div class="col-sm-3 col-xs-6">
<article class="product-item">
<div class="row">
<div class="col-sm-3">
<div class="product-overlay">
<div class="product-mask"></div>
<img src="<?php echo '../admin/documentos/'.$regi['nome_doc']; ?>" style="width:100%">
</div>
</div>
<div class="col-sm-9">
<div class="product-body">
<h3><?php echo $regi['nome']; ?></h3>
<br>
<span class="price">
<ins><span class="amount"><?php echo $regi['preco']; ?></span></ins>
</span>
<div class="buttons buttons-simple">
<i class="fa fa-shopping-cart"></i>Comprar bilhetes
</div>
</div>
</div>
</div>
</article>
</div>
<?php
} else{
?>
<p>Nada!</p>
<?php
}
}
?>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
The reason you are getting the error because you have while loop in your if block that you are not closing.
Check the code below for this fixed version.
<?php
$sqli=sprintf("SELECT * FROM eventos WHERE YEAR(data) = 2027");
$resu=mysqli_query($con,$sqli);
mysqli_set_charset($con, 'UTF8');
if (mysqli_num_rows($resu)>0):
while($regi=mysqli_fetch_array($resu)):
$sqli_consulta=sprintf("select * from eventos where id=%d;",$regi['id']);
$resu_consulta=mysqli_query($con,$sqli_consulta);
$regi_consulta=mysqli_fetch_array($resu_consulta);
$linkk='../eventoindividual.php?id='.$regi_consulta['id'];
?>
<div class="col-sm-3 col-xs-6">
<article class="product-item">
<div class="row">
<div class="col-sm-3">
<div class="product-overlay">
<div class="product-mask"></div>
<img src="<?php echo '../admin/documentos/'.$regi['nome_doc']; ?>" style="width:100%">
</div>
</div>
<div class="col-sm-9">
<div class="product-body">
<h3><?php echo $regi['nome']; ?></h3>
<br>
<span class="price">
<ins><span class="amount"><?php echo $regi['preco']; ?></span></ins>
</span>
<div class="buttons buttons-simple">
<i class="fa fa-shopping-cart"></i>Comprar bilhetes
</div>
</div>
</div>
</div>
</article>
</div>
<?php endwhile; else: ?>
<p>Nada!</p>
<?php
endif;
?>
Move the last } bracket above }else{
<section class="content products">
<div class="container">
<h1 class="hidden">Eventos</h1>
<div class="row">
<div class="col-sm-10">
<div class="row grid" id="products">
<div class="releated-products">
<div class="row grid" id="products">
<?php
$sqli=sprintf("SELECT * FROM eventos WHERE YEAR(data) = 2027");
$resu=mysqli_query($con,$sqli);
mysqli_set_charset($con, 'UTF8');
if (mysqli_num_rows($resu)>0) {
while($regi=mysqli_fetch_array($resu)){
$sqli_consulta=sprintf("select * from eventos where id=%d;",$regi['id']);
$resu_consulta=mysqli_query($con,$sqli_consulta);
$regi_consulta=mysqli_fetch_array($resu_consulta);
$linkk='../eventoindividual.php?id='.$regi_consulta['id'];
?>
<div class="col-sm-3 col-xs-6">
<article class="product-item">
<div class="row">
<div class="col-sm-3">
<div class="product-overlay">
<div class="product-mask"></div>
<img src="<?php echo '../admin/documentos/'.$regi['nome_doc']; ?>" style="width:100%">
</div>
</div>
<div class="col-sm-9">
<div class="product-body">
<h3><?php echo $regi['nome']; ?></h3>
<br>
<span class="price">
<ins><span class="amount"><?php echo $regi['preco']; ?></span></ins>
</span>
<div class="buttons buttons-simple">
<i class="fa fa-shopping-cart"></i>Comprar bilhetes
</div>
</div>
</div>
</div>
</article>
</div>
<?php
} // while()
} else{
?>
<p>Nada!</p>
<?php
}
// } //wrong bracket
?>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
For your own sanity it is a good idea leave a comment after closing bracket of a long code, so you know which block it belongs to.
Compiler can't be wrong, if it says there's syntax error then there is. The bracket before the else is closing the while loop - hence else cannot be used there, close the if condition. Here's the corrected code:
<section class="content products">
<div class="container">
<h1 class="hidden">Eventos</h1>
<div class="row">
<div class="col-sm-10">
<div class="row grid" id="products">
<div class="releated-products">
<div class="row grid" id="products">
<?php
$sqli=sprintf("SELECT * FROM eventos WHERE YEAR(data) = 2027");
$resu=mysqli_query($con,$sqli);
mysqli_set_charset($con, 'UTF8');
if (mysqli_num_rows($resu)>0) {
while($regi=mysqli_fetch_array($resu)) {
$sqli_consulta=sprintf("select * from eventos where id=%d;",$regi['id']);
$resu_consulta=mysqli_query($con,$sqli_consulta);
$regi_consulta=mysqli_fetch_array($resu_consulta);
$linkk='../eventoindividual.php?id='.$regi_consulta['id'];
/* 2 brackets { */
?>
<div class="col-sm-3 col-xs-6">
<article class="product-item">
<div class="row">
<div class="col-sm-3">
<div class="product-overlay">
<div class="product-mask"></div>
<img src="<?php echo '../admin/documentos/'.$regi['nome_doc']; ?>" style="width:100%">
</div>
</div>
<div class="col-sm-9">
<div class="product-body">
<h3><?php echo $regi['nome']; ?></h3>
<br>
<span class="price">
<ins><span class="amount"><?php echo $regi['preco']; ?></span></ins>
</span>
<div class="buttons buttons-simple">
<i class="fa fa-shopping-cart"></i>Comprar bilhetes
</div>
</div>
</div>
</div>
</article>
</div>
<?php
} // while loop
} // if condition
else {
?>
<p>Nada!</p>
<?php
}
?>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
hope this helps. 👍

bootstrap 4 with codeignitor form with validation

Not showing responsive in safari browser Responsive issue
I am unable to control to responsive this code, working fine in windows chrome and Mozilla, but not on mac. can anyone help me?
<?php echo form_open('user/signup');?>
<div class="form-row">
<div class="form-group">
<div class="col-md-6 col-xs-12 col-sm-12">
<div class="form-label-group">
<?php echo form_input(['name'=>'firstname','id'=>'firstname','class'=>'form-control','autofocus'=>'autofocus','value'=>set_value('firstname')]);?>
<?php echo form_label('Enter your first name', 'firstname'); ?>
<?php echo form_error('firstname',"<div style='color:red'>","</div>");?>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-xs-12 col-sm-12">
<div class="form-label-group">
<?php echo form_input(['name'=>'lastname','id'=>'lastname','class'=>'form-control','autofocus'=>'autofocus','value'=>set_value('lastname')]);?>
<?php echo form_label('Enter your last name', 'lastname'); ?>
<?php echo form_error('lastname',"<div style='color:red'>","</div>");?>
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="form-label-group">
<?php echo form_input(['name'=>'emailid','id'=>'emailid','class'=>'form-control','autofocus'=>'autofocus','value'=>set_value('emailid')]);?>
<?php echo form_label('Enter valid email id', 'emailid'); ?>
<?php echo form_error('emailid',"<div style='color:red'>","</div>");?>
</div>
</div>
<div class="form-group">
<div class="form-label-group">
<?php echo form_input(['name'=>'mobilenumber','id'=>'mobilenumber','class'=>'form-control','autofocus'=>'autofocus','value'=>set_value('mobilenumber')]);?>
<?php echo form_label('Enter Mobile Number', 'mobilenumber'); ?>
<?php echo form_error('mobilenumber',"<div style='color:red'>","</div>");?>
</div>
</div>
<div class="form-group">
<div class="form-row">
<div class="col-md-6">
<div class="form-label-group">
<?php echo form_password(['name'=>'password','id'=>'password','class'=>'form-control','autofocus'=>'autofocus','value'=>set_value('password')]);?>
<?php echo form_label('Password', 'password'); ?>
<?php echo form_error('password',"<div style='color:red'>","</div>");?>
</div>
</div>
<div class="col-md-6">
<div class="form-label-group">
<?php echo form_password(['name'=>'confirmpassword','id'=>'confirmpassword','class'=>'form-control','autofocus'=>'autofocus','value'=>set_value('confirmpassword')]);?>
<?php echo form_label('Confirm Password', 'confirmpassword'); ?>
<?php echo form_error('confirmpassword',"<div style='color:red'>","</div>");?>
</div>
</div>
</div>
</div>
<?php echo form_submit(['name'=>'Register','value'=>'Register','class'=>'btn btn-primary btn-block']); ?>
<?php echo form_close(); ?>
How do I solve this issue
Responsive issue on safari
100% working in windows chrome

Edit option from Radio Buttons in PHP

I am VERY new to PHP and editing some code
Currently it has two radio buttons. Selecting either will bring up some options.
I am wanting to get rid of one option (pick up) and have the page just show the delivery option.
Any help would be ace, I have tried playing with the code but no luck!
<div class="top10 row">
<div class="col-xs-6 ">
<?php echo CHtml::radioButton('trans_type',false,array(
'class'=>"trans_type",
'value'=>'pickup',
'required'=>true
));
?>
<span><?php echo Driver::t("Pickup")?></span>
</div>
<div class="col-xs-6 ">
<?php echo CHtml::radioButton('trans_type',false,array(
'class'=>"trans_type",
'value'=>"delivery"
));
?>
<span><?php echo Driver::t("Delivery")?></span>
</div> <!--col-->
</div> <!--row-->
<div class="delivery-info top20">
<div class="row">
<div class="col-sm-6">
<?php echo CHtml::textField('contact_number','',array(
'class'=>"mobile_inputs",
'placeholder'=>Driver::t("Contact nunber"),
'maxlength'=>15
))?>
</div> <!--col-->
<div class="col-sm-6 ">
<?php
echo CHtml::textField('email_address','',array(
'placeholder'=>Driver::t("Email address")
))
?>
</div> <!--col-->
</div> <!--row-->
<div class="row top10">
<div class="col-sm-6 ">
<?php echo CHtml::textField('customer_name','',array(
'placeholder'=>Driver::t("Name"),
'required'=>true
))?>
</div>
<div class="col-sm-6 "><?php echo CHtml::textField('delivery_date','',array(
'placeholder'=>Driver::t("Delivery before"),
'required'=>true,
'class'=>"datetimepicker"
))?></div>
</div> <!--row-->
<div class="row top10">
<div class="col-sm-12 ">
<?php
$map_provider = Driver::getMapProvider();
?>
<?php if ($map_provider =="mapbox"):?>
<div id="mapbox_delivery_address" class="mapbox_geocoder_wrap"></div>
<?php elseif ( $map_provider=="google.maps"):?>
<?php
echo CHtml::textField('delivery_address','',array(
'class'=>'delivery_address geocomplete delivery_address_task',
'placeholder'=>Driver::t("Delivery Address"),
'required'=>true
));
?>
<?php endif;?>
Remove the lines of code below (or comment them out while you try it). Make a copy of the file before you make any changes in case you need to back out the attempt.
<div class="col-xs-6 ">
<?php echo CHtml::radioButton('trans_type',false,array(
'class'=>"trans_type",
'value'=>'pickup',
'required'=>true
));
?>
<span><?php echo Driver::t("Pickup")?></span>
</div>

2 different post layouts in a loop

I have just created thus BEAUTIFUL loop
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="row sectionrow" onclick="void(0)">
<div class="col-sm-4 sectionrecipes" style="background-image:url('<?php echo the_field(background_image_title); ?>');">
<div class="textsmall" >
<?php the_field(titlebreak); ?>
</div>
</div>
<div class="col-sm-8">
<div class="sectionrecipes" style="background-image:url('<?php echo the_field(background_image_detail); ?>');">
<div class="textbigrecipes">
<div class="inner">
<?php the_excerpt(); ?>
<button type="button" class="btn btn-default">READ MORE</button>
</div>
</div>
</div>
</div>
</div>
</div>
But I would like to invert the positon of the bootstrap divs on the second post and return to orginal on the third and ....
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="row sectionrow" onclick="void(0)">
<div class="col-sm-8">
<div class="sectionrecipes" style="background-image:url('<?php echo the_field(background_image_detail); ?>');">
<div class="textbigrecipes">
<div class="inner">
<?php the_excerpt(); ?>
<button type="button" class="btn btn-default">READ MORE</button>
</div>
</div>
</div>
</div>
</div>
<div class="col-sm-4 sectionrecipes" style="background-image:url('<?php echo the_field(background_image_title); ?>');">
<div class="textsmall" >
<?php the_field(titlebreak); ?>
</div>
</div>
</div>
Is it possible?
1. First declare a variable Outside the loop (like as $i =1),
Then check the condition.
Mode of i
example:
if($i% 2 == 0) {
-----layout First code-----
}else
{
-----layout Second code-----
}
Increment the variable value ($i++)

Using a foreach loop within coloumn data

How Can I make it so that it only does foreach on the data and not the html columns?
<div class="columns">
<div class="title">Donation Amount</div>
<div class="title">Support</div>
<div class="title">View Grant</div>
<?php foreach ($results->result() as $grant) : ?>
<div class="col1"><p>AUD $<?php echo number_format($grant->user_budget);?></p></div>
<div class="col2"></div>
<div class="col3"></div>
</div>
<div class="clear"></div>
<?php endforeach ; ?>
Try this
<div class="columns">
<div class="title">Donation Amount</div>
<div class="title">Support</div>
<div class="title">View Grant</div>
<?php foreach ($results->result() as $grant) : ?>
<div class="col1"><p>AUD $<?php echo number_format($grant->user_budget);?></p></div>
<div class="col2"></div>
<div class="col3"></div>
<div class="clear"></div>
<?php endforeach ; ?>
</div>

Categories