How to create an if statement in PHP based on HTML values - php

I'm quite new to php, so be gentle.....
I have a basic list view that I have set up to match my html/css styles on a sidebar.....but every few listgroup items there are 'header' items on the list that do not inherit the same styles as other listgroup items.
For example, one list group item would have checkboxes and save icons where as the heading list group item would be the same size but would have bold text and no buttons.
This is what I have. On the section below the main one, I have the HTML values for a header list group item. Is it possible to tell the PHP to detect 'step-heading' as a class and then display the following code for all relevant values?
EDIT: I know that in the if-statement that $step-heading is invalid because I have not declared it as a variable...I simply have no idea what to put there otherwise.
<?php foreach ($tasks as $i => $task) { ?>
<li class="list-group-item" tabindex="-1">
<a class="listgroupwrap" href="#g<?= $i + 1 ?>">
</a>
<span class="step-number-container"> <?=$i + 1 ?>
</span>
<span class="btn-check">
<i class="glyphicon glyphicon-ok"></i>
</span>
<!--<span class="step-checkbox"></span>-->
<span class="step-name"><?php echo $task["name"] ?>
</span>
<span class="step-show-hide">
<span class="btn">
<i class="glyphicon glyphicon-chevron-right">
</i>
</span>
</span>
</li>
<?php } ?>
<?php
if ( $tasks == "step-heading" ) {
?>
<li class="list-group-item step-heading" tabindex="-1">
<span class="step-number-container">2</span>
<a class="listgroupwrap" href="#g<?= $i + 1 ?>">
</a>
<span class="step-name">Pre-meeting email, send below items:</span>
</li>
<?php;
}
?>

You can't have it both ways:
<?php foreach ($tasks as $i => $task) { ?>
^^^^^^---array
if ( $tasks == "step-heading" ) {
^^^^^^---array-as-string
An array in string context is simply the literal word Array.
Perhaps you mean:
if ( $task == "step-heading" ) {
^---no S
instead.

Related

How do I remove the last <hr> tag in a foreach loop in Codeigniter? [duplicate]

This question already has answers here:
Find the last element of an array while using a foreach loop in PHP
(35 answers)
Closed 2 years ago.
Sorry for posting like this but I am learning
I want all the results to have a hr or any html based line except the last one
I put a line break but it won't look good
help me
foreach ($newsqry->result() as $n) {
$nr=$n->numrows();
$nlink = array_keys($routesAll, "news/index/" . $n->n_id);
?>
<div class="row">
<div class="col-md-6 imgpost">
<a class="post-imgn" href="<?=site_url($nlink)?>">
<img class=""src="<?=base_url("assets/uploads/news/thmb/$n->img")?>"alt="<?=character_limiter($n->title,'50')?>">
</a>
</div>
<div class="col-md-6 descpost">
<div class="post-category com" >
<?php
$t = $n->tags;
$s = explode(',', $t);
foreach ($s as $a) {
?>
<a class="tags" href="<?=site_url("news/view?tag=".strtolower(str_replace(' ','-',$a)))?>"><?=ucwords($a)?></a>
<?php } ?>
</div>
<h3 class="post-title news">
<?=character_limiter($n->title,'100')?>"
</h3>
<ul class="post-meta">
<li><?=$n->author?></li>
<li><i class="fa fa-clock-o" aria-hidden="true"></i> <?=date("h:i A",strtotime($n->time))?> <?=date("F-d-y",strtotime($n->date))?></li>
</ul>
<p class="descfornews"><?=character_limiter(strip_tags($n->desc),'190')?></p>
</div>
</div>
<?php if($nr<1){echo "<hr>";}else{ echo "<h3>End of Page</h3>";}?>
<?php
}
?>
I think you can do something like this.
it will print before the data printed, but there is $data variable to prevent it to print before the first data.
<?php
$data = 0;
$t = $n->tags;
$s = explode(',', $t);
foreach ($s as $a) {
if($data!=0){echo "<hr>";}
?>
<a class="tags" href="<?=site_url("news/view?tag=".strtolower(str_replace(' ','-',$a)))?>"><?=ucwords($a)?></a>
</div>
<h3 class="post-title news"><?=character_limiter($n->title,'100')?>"</h3>
<ul class="post-meta">
<li>
<?=$n->author?>
</li>
<li>
<i class="fa fa-clock-o" aria-hidden="true"></i>
<?=date("h:i A",strtotime($n->time))?>
<?=date("F-d-y",strtotime($n->date))?></li>
</ul>
<p class="descfornews"><?=character_limiter(strip_tags($n->desc),'190')?></p>
</div>
</div>
<?php
}
echo("<h3>End of Page</h3>");
?>

Add to favorites functionality using Jquery in CodeIgniter framework

I am currently working on a tour website, where the number of destinations is available for the user. A Login user can like any destination which will be added to his favorites destination section. I have done this task successfully. But after liked the destination the like button appears multiple times or equal to the number of likes have the user done. Maybe I am wrong in my logic here is my code. The first loop is fetching all the destination and the second loop fetching the favorites destination.
Note:-Here I am showing only logic code not complete HTML or other PHP code
<?php foreach($listing_data as $list){ ?>
<?php foreach($favorites as $fav){ if($fav['link_id']==$list['id']){?>
<li class="pull-right"> <a class="Theme" id="liked"><i class="fas fa-heart"></i> </a> </li>
<?php } else {?>
<li class="pull-right"> <i class="far fa-heart"></i> </li>
<?php } } }?>
Just add a helper variable
and take out the html in 2th foreach.
$like = false; in each Site
Then in each site you loop $favorites, then walk in $favorites for check if TheSite is liked;
after second foreach you should compare if $like is true for use class "fas" or false for use class "far"
<?php foreach($listing_data as $list){
$like = false;
foreach($favorites as $fav){
if($fav['link_id']==$list['id']){
$like = true;
}
}
if($like){ ?>
<li class="pull-right">
<a class="Theme" id="liked"><i class="fas fa-heart"></i></a>
</li>
<?php } else { ?>
<li class="pull-right">
<i class="far fa-heart"></i>
</li>
<?php
}
}
?>
This is just a refactoring of the code to make it look a bit cleaner.
The links are not included as they are ? and also use id's that will not be unique.
So the code could become something like...
<?php foreach ($listing_data as $list): ?>
<?php
foreach ($favorites as $fav) {
$like = ($fav['link_id'] == $list['id']);
}
?>
<li class="pull-right">
<?php if ($like): ?>
<span>More Sensible Link 1</span>
<?php else: ?>
<span>More Sensible Link 2</span>
<?php endif; ?>
</li>
<?php endforeach; ?>

nested replies to a comment using recursion php

I have a comment and reply system in which I want to nest replies in an indented manner using recursion. I am getting all the comments and their replies in a normal manner without indentation and everything displayed in an order according to id.
I have a column in the comment table type which tells whether it is a comment or reply and another column which stores parentId of the reply.
I have tried something like
<?php displayComments($comment);
function displayComments($comment) {
foreach($comment as $value) { ?>
<ul class="commentList">
<li>
<div class="commentData" id="<?php echo $value['id']; ?>">
<p><?php echo $value['author']; ?><span class="date"><?php echo $value['date']; ?></span></p>
<p><?php echo $value['content']; ?></p>
<p><span class="reply">Reply</span> <i class="fa fa-thumbs-up" aria-hidden="true" tabindex="1"></i> <i class="fa fa-thumbs-down" aria-hidden="true" tabindex="1"></i> </p>
</div>
</li>
</ul>
<?php }
} ?>
where $comment is an array which stores all the comment and replies.
Please help me how can i display the replies in a nested manner if I know which is a reply and the parentId of the reply

PHP 7 - RecursiveArrayIterator Trying to construct a drop down menu using a multi-dimensional array with string and array values

PHP 7
Intermediate to beginner level coder.
I have researched around pretty thoroughly and unable to find a solution on SO that fits my issue completely. I found very similar, but just different enough so I am unable to make it work out.
I am trying to construct a nav bar in Bootstrap 3 using results from a DB query. The results have a mix of string values along with array values. I have worked out most of the construct, but I am unable to get the child array values to insert into and display the results into the drop down menu properly. I am using the RecursiveArrayIterator function with ->getChilden() to filter out the arrays and show the ->hasChildren() values.
PROBLEM:
My drop down lists are only displaying the word array, they are not parsing in the value of the array.
The drop down is also getting all array values that are a child and placing them in both buttons, at least that is what I think is happening.
Here is what I want the menu to look like:
Link to Bootply
I am able to get the structure perfect for the navbar and parent elements, the drop down works, however, I am getting 'array' results in the drop down, I am also getting a combination of both array results as I have yet to figure out how to filter out the arrays with children to parse the proper info to the proper arrays drop down section.
Here is a screen shot:
*Here is the DB query in array:
Only two arrays are coming from this query, CUSTOMERS and CUST_ROUTE_DAY
$custRouteDay and $custName are variables from a while loop of the fetch_assoc() results.
define("CUST_ROUTE_DAY", $custRouteDay);
define("CUSTOMERS", $custName);
define("NAVBTNS", [
"Customers"=>CUSTOMERS,
"Route Info"=>CUST_ROUTE_DAY,
"Tasks",
"Networking",
"Maps"
]);
Here is the resulting array that comes from the DB.
NAVBTNS = Array ( [Customers] => Array ( [0] => John Banner [1] => Dale
Landry [2] => Bill Childers [3] => Darren Little ) [Route Info] => Array (
[0] => 1 [1] => 3 [2] => 3 [3] => 4 ) Tasks Networking Maps )
Basically there are two functions, one to construct the navbar and another to construct the drop down buttons.
function construct_navbar_buttons(){
$navbtns = NAVBTNS;
$iterator = new RecursiveArrayIterator($navbtns);
$btns = '<ul class="nav navbar-nav">';
$i = 0;
while ($iterator->valid()) {
if ($iterator->hasChildren()) {
$btnNm = array();
//Get the names for the dropdown (Arrays) which, for the
//arrays will = the $key value and place them into $btnNm[]
foreach(NAVBTNS as $btnName=>$result){
$btnNm[]=$btnName;
$results[]=$result;
}
$btns .= '<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="">
<i class="'.NAVICONS[$i].'">
</i> '.$btnNm[$i].' <span class="caret"> </span>
</a>
<ul class="dropdown-menu dropdown-user">';
//Get the values within the child arrays
foreach($row = $iterator->getChildren() as $key => $btn){
$count = count($row);
for($inc=1;$inc<$count;$inc++){
$btns .= '<li>'.$results[$i].'</li>';
}
}
$btns .= '</ul>
</li>';
}else{
foreach($iterator as $num => $value){
if(!is_array($value)){
$btns .= '<li>'.$value.'</li>';
}
}
}
$i++;
$iterator->next();
}
$btns .= '</ul>';
return $btns;
}
function construct_navbar(){
$stmt = '
<!--NAV BAR IS TOP FIXED-->
<nav role="navigation" class="'.STYLE["fixed-top"].'">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<a class="navbar-brand" href="#">Pool Buddy Pro</a>
</div>';
$stmt .= construct_navbar_buttons();
$stmt .= '
<ul class="nav navbar-nav nav-pills navbar-right">
<li>Link</li>
<li class="dropdown">
<i class="glyphicon glyphicon-user"></i> <i class="caret"></i></span>
<ul class="dropdown-menu">
<li><i class="glyphicon glyphicon-cog"></i> Edit User Account Info</li>
<li><i class="glyphicon glyphicon-search"></i> Search Customers</li>
<li><i class="fa fa-eye"></i> View Your Network</li>
<li role="separator" class="divider"></li>
<li><i class="glyphicon glyphicon-log-out" aria-hidden="true"></i> Logout</li>
<!--<li role="separator" class="divider"></li>
<li>One more separated link</li>-->
</ul>
</li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>';
return $stmt;
}
HTML:
<body>
<div class="row">
<div style="margin-bottom:0px;">
<?php echo construct_navbar(); ?>
</div>
<div style="padding-top:50px;padding-left:0px;" class="row">
<div class="sidebar-nav navbar-collapse col-lg-4">
<div style="box-shadow:1px 0px 4px #999;background-color:#F8F8F8;padding-top:20px !important;" class="col-lg-12 sidebar-nav navbar-collapse">
<?php construct_side_bar_customer_nav(); ?>
</div>
</div>
<div class="col-lg-8">
<div class="heading col-lg-12">
...CONTENT...
</div>
</div>
</div>
</div>
</body>
So very close, any help would be greatly appreciated!
Better apporach to iterate here.
You have an array with stringvalues OR subarrays
So do
//this 2 arrays can created just once, content never changes
foreach(NAVBTNS as $btnName=>$result){
$btnNm[]=$btnName;
$results[]=$result;
}
//$iterator=$navbtns
foreach($iterator as $k => $v){//$k holds index or e.g. Customers
//You can use $k here for the <i></i> tag
if(is_array($v)){//go over subarray
$btns .= '<li class="dropdown">....</i> '.$k.' ';
foreach($v as $key => $value){
//do stuff example data: [0] => John Banner
$btns .= '<li>'.$value.'</li>';
}
$btns .= "</ul>\n</li>";
} else {//whe have a string value
//do stuff example data: Tasks - key here seems aways a index number
$btns .= '<li>'.$v.'</li>';
}
}
More is not needed, because you will never get an array->sub-array->sub-sub-array.
I have problems to get the logic of ?p='.$i.' here.

How can set different arrays that deals with different range of values?

I want to achieve the following html dynamically:
Time period AD:
<ul>
<li>1200</li>
<li>1300
<ul>
<li>1301</li>
</ul>
<li>
</ul>
Time period BC:
<ul>
<li>-200</li>
<li>-450
<ul>
<li>-451</li>
</ul>
</li>
</ul>
In the following I have set an array so that I can, by using some custom fields, push my dates into the array, then tell the whole code to calculate in 100 of years, in order to be able to place years like 1301 as a nested ul under 1300.
<ul>
<?php
$yearsArray = [];
$centuryHash = [];
query_posts(array(
'post_type' => 'post',
'posts_per_page' => -1
));
while ( have_posts() ) : the_post();
array_push($yearsArray, get_field("year"));
if (($wp_query->current_post +1) == ($wp_query->post_count)) {
$yearsArray = array_unique($yearsArray);
sort($yearsArray);
}
endwhile;
foreach ($yearsArray as $year) {
$currentCentury = floor($year/100)*100;
if(!$centuryHash[$currentCentury]){
$centuryHash[$currentCentury] = [];
}
if($currentCentury != $year){
$centuryHash[$currentCentury][] = $year;
}
}
foreach ($centuryHash as $century => $centuryYears) { ?>
<li class="dropdown">
<?php if($centuryYears){ ?>
<a class="btn btn-default" href="#" class="dropdown-toggle" data-date="<?php echo $century; ?>" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
<?php echo $century; ?>
<span class='caret'></span>
</a>
<ul class='dropdown-menu'>
<?php foreach ($centuryYears as $year) { ?>
<li>
<a class="btn btn-default" data-date="<?php echo $year; ?>" href="#">
<?php echo $year; ?>
</a>
</li>
<?php }
echo "</ul>";
} else { ?>
<a class="btn btn-default" data-date="<?php echo $century; ?>" href="#">
<?php echo $century; ?>
</a>
<?php } ?>
</li>
<?php }
?>
</ul>
The problem that I can't figure out is how I can say like (verbal code):
If value is within the range of -450 till 2 then go into this array otherwise go in that array instead
From my end I could set in the cms (people will be able to insert content with dates) some flags so that I can do simple conditionals like (verbal code):
"Is this date from a or b? then do this or that"
But that creates an issue because I could flag a period of time and place the wrong date in the wrong period.
Therefore the last solution that I thought is to set a range of periods of times and delimiter some arrays by create different ones where I could dynamically push the values depending on their values.
This could be a start maybe (example from docs)? But how can I set an if to control what goes there or not?
foreach (range(0, 12) as $number) {
echo $number;
}
Here is a simple answer to your question.
If value is within the range of -450 till 2 then go into this array otherwise go in that array instead
if ($value >= -450 && $value <= 2) {
// do this
} else {
// do that
}
Hope, this answers to your question (which is, unfortunately, overloaded with dozens of code that doesn't apply to the question).

Categories