Hello I want to change the content on the amount of posts so if the foreach loop got only 2 posts it should say 2. But it is not working?
<?php if (!empty($iconMenu)) { ?>
<nav class="menu-top">
<?php $i = 0;
foreach($iconMenu as $icon) {
$page = Website::getActiveTreeBranch($icon['link']);
if ($page !== false) {
$i++;
if($i=1){ ?>
1
<?php } ?>
<?php if($i=2){ ?>
2
<?php } ?>
<?php }} ?>
<?php } ?>
</nav>
Simple Typo:
= is assignment operator and == is equality check operator.
Change code:
if($i=1){ ?>
1
<?php } ?>
<?php if($i=2){ ?>
To
if ($i == 1) {
echo '1'
}
if($i == 2) {
So, the final (cleaned up code):
<?php
if (! empty($iconMenu)) {
echo '<nav class="menu-top">';
$i = 0;
foreach ($iconMenu as $icon) {
$page = Website::getActiveTreeBranch($icon['link']);
if ($page !== false) {
$i++;
if ($i == 1) {
echo '1';
}
if ($i == 2) {
echo '2 ';
}
}
}
}
echo '</nav>';
?>
This answer is not for the question but to help him how to use the php tag.
<?php $i = 0;
foreach($iconMenu as $icon) {
$page = Website::getActiveTreeBranch($icon['link']);
if ($page !== false) {
$i++;
if($i==1){
Echo "1";
}
if($i==2){
echo "2";
}
}}
} ?>
</nav>
I have not double checked if there is a correct number of { and } as I'm typing on my phone.
EDIT: I forgot to edit the thing that was wrong if =
I fixed it, I had to use Count instead.
Related
This site (https://www.helpendhunger.org/find-food-assistance/) has an input for zip search, and one would assume it returns locations based on corresponding zip codes. But I don't think that's the case.
Looking at the code below, the filter looks like it uses associated latitude and longitude values to determine the results. Still, the zip code must be coming into play somewhere, but I can't figure out how. Can anybody help me understand what's going on here?
Here's the main portion I'm concerned with, followed by the full code.
<?php
case 'zip':
$none = !empty($_REQUEST["none"]) ? $_REQUEST['none'] : 'false';
$lat = $_REQUEST["lat"];
$lng = $_REQUEST["lng"];
$x = 0;
unset($loc_array);
$loc_array = array();
foreach ($locations as $loc) {
$radius = 35;
$zip_location = $loc['zip_code'];
$lat_loc = $loc['latitude'];
$lng_loc = $loc['longitude'];
$distance = distance($lat, $lng, $lat_loc, $lng_loc, "M");
if ($distance == $radius) {
array_push($loc_array, $loc);
}
}
$count = count($loc_array);
if ($count > 0 && $none == 'false') { ?>
<div class="table">
<?php foreach ($loc_array as $location) {
include ('../lib/locations.php');
} ?>
<script> var _jsonNew = <?php echo json_encode($loc_array) ?>; </script>
<?php } else { ?>
<div class="no_results">
<div class="in">
<div class="response">There were no results for this zip code, please try again.<br><span>or</span></div>
<div class="button" data-county="all">View All Locations</div>
</div>
</div>
<script> var _jsonNew = <?php echo json_encode($locations) ?>; </script>
<?php } ?>
Full code:
<?php
require("../wp-load.php");
global $wpdb;
$oper = !empty($_REQUEST["oper"]) ? $_REQUEST["oper"] : die("No Oper");
$sql = "SELECT * FROM locations ORDER BY ID DESC";
$locations = $wpdb->get_results($sql,ARRAY_A);
function sortLocs($a, $b) {
return ($a["location_title"] > $b["location_title"]) ? 1 : -1;
}
usort($locations, "sortLocs");
if (!function_exists("stripslashes_deep")) {
function stripslashes_deep($value)
{
if (is_array($value)) {
$value = array_map('stripslashes_deep', $value);
} else {
$value = trim($value);
$value = stripslashes($value);
}
return $value;
}
}
$locations = stripslashes_deep($locations);
switch($oper){
case 'county':
$county_request = !empty($_REQUEST["county"]) ? $_REQUEST["county"] : die("No County");
$county = strtolower($county_request);
$x = 0;
unset($loc_array);
$loc_array = array();
foreach ($locations as $loc) {
$county_location = strtolower($loc['county']);
if ($county_location == $county) {
array_push($loc_array,$loc);
}
if ($county=='all') {
array_push($loc_array,$loc);
}
}
$count = count($loc_array); ?>
<div class="table">
<?php
foreach($loc_array as $location) {
include ('../lib/locations.php');
}
?>
<script>
var _jsonNew = <?php echo json_encode($loc_array) ?>;
</script>
<?php
break;
case 'zip':
$none = !empty($_REQUEST["none"]) ? $_REQUEST['none'] : 'false';
$lat = $_REQUEST["lat"];
$lng = $_REQUEST["lng"];
$x = 0;
unset($loc_array);
$loc_array = array();
foreach ($locations as $loc) {
$radius = 35;
$zip_location = $loc['zip_code'];
$lat_loc = $loc['latitude'];
$lng_loc = $loc['longitude'];
$distance = distance($lat, $lng, $lat_loc, $lng_loc, "M");
if ($distance == $radius) {
array_push($loc_array, $loc);
}
}
$count = count($loc_array);
if ($count > 0 && $none == 'false') { ?>
<div class="table">
<?php foreach ($loc_array as $location) {
include ('../lib/locations.php');
} ?>
<script> var _jsonNew = <?php echo json_encode($loc_array) ?>; </script>
<?php } else { ?>
<div class="no_results">
<div class="in">
<div class="response">There were no results for this zip code, please try again.<br><span>or</span></div>
<div class="button" data-county="all">View All Locations</div>
</div>
</div>
<script> var _jsonNew = <?php echo json_encode($locations) ?>; </script>
<?php } ?>
<?php break;
}
?>
It's difficult to tell, but it seems as if there's something that's converting the zip code to a lat/lng (easily done with Google Maps in JS), and must be doing a center-of-poly search to the lat/lng of the locations they're supporting.
Here's the thing: I have a foreach loop that adds inputs dynamically. I need it to place part of them in one div, the rest in another. The current code is the following:
$sqla=mysql_fetch_row($sql);
$x=1;
if($x<50)
{
?>
<div class="area area-1">
<?
foreach($sqla as $key=>$values){
if ($key == "0") {
continue;
}
$icheck = ($values > 0) ? "icheck" : "";
$ichecked = ($values > 0) ? "isChecked" : "";
echo "<label class='label-area label-".$values['num".$x."'][$x]." ".$ichecked."'><input name='data[ar][".$x."][]' type='checkbox' value='".$x."' title='".$x."' class='".$icheck." archeck1'><span class='label-num'>".$x."</span><span class='label-check-mark'></span></label>";
if ($key == "50") {
break;
}
$x++;
if ($values > 0) {
$new_rand_arr[] = $values;
}
}
?>
</div>
<?
}else{
?>
<div class="zodiak ar-1">
<?
foreach($sqla as $key=>$values){
if ($key == "50") {
continue;
}
$icheck = ($values > 0) ? "icheck" : "";
$ichecked = ($values > 0) ? "isChecked" : "";
echo "<label class='label-area label-".$values['num".$x."'][$x]." ".$ichecked."'><input name='data[ar][".$x."][]' type='checkbox' value='".$x."' title='".$x."' class='".$icheck." archeck1'><span class='label-num'>".$x."</span><span class='label-check-mark'></span></label>";
if ($key == "62") {
break;
}
$x++;
if ($values > 0) {
$new_rand_arr[] = $values;
}
}
?>
</div>
<?
}
?>
The output puts it all in the first div, but none in the "zodiak ar-1" one. The target thing is everything after the 50-th key to go into that div. Hope that managed to explain the issue...
Thank you
Right now you are doing this:
$x=1;
if($x<50)
{
// your code
} else {
// your code
}
The problem is that you do a foreach INSIDE the if statement, so $x < 50 will ALWAYS be true because just before you do $x = 1.
Now in both foreach loop you do this :
foreach($sqla as $key=>$values){
if ($key == "0") {
continue;
}
// your code
}
foreach($sqla as $key=>$values){
if ($key == "50") {
continue;
}
// your code
}
So you use a var $x that you increment each turn but you have a $key that you use too to check if value is <50 or not?
So try something like this :
$new_rand_arr = array();
$open_first_div = false;
$open_second_div = false;
$html = "";
foreach($sqla as $key=>$values){
if ($key < "50") {
// You open your first div one time
if (!$open_first_div) {
$html .= "<div class=\"area area-1\">";
$open_first_div = true;
}
$icheck = ($values > 0) ? "icheck" : "";
$ichecked = ($values > 0) ? "isChecked" : "";
html .= "<label class='label-area label-".$values['num".$x."'][$x]." ".$ichecked."'><input name='data[ar][".$x."][]' type='checkbox' value='".$x."' title='".$x."' class='".$icheck." archeck1'><span class='label-num'>".$x."</span><span class='label-check-mark'></span></label>";
if ($values > 0) {
$new_rand_arr[] = $values;
}
} else {
// You close your first div and open the second div
if (!$open_second_div) {
$html .= "</div><div class=\"zodiak ar-1\">";
$open_second_div = true;
}
$icheck = ($values > 0) ? "icheck" : "";
$ichecked = ($values > 0) ? "isChecked" : "";
$html .= "<label class='label-area label-".$values['num".$x."'][$x]." ".$ichecked."'><input name='data[ar][".$x."][]' type='checkbox' value='".$x."' title='".$x."' class='".$icheck." archeck1'><span class='label-num'>".$x."</span><span class='label-check-mark'></span></label>";
if ($values > 0) {
$new_rand_arr[] = $values;
}
}
}
// After the foreach your close your div
$html .= "</div>";
// You display it
echo $html;
This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 4 years ago.
I'm new to PHP, please be gentle.
What do I need to change to make this work in PHP?
<div>some HTML here</div>
<?php
function typ__1() {
if ($temperature >= 29) {
$hot = true;
} else {
$hot = false;
}
}
?>
<?php foreach (array_slice($data->something->something, 0, 5) as $day):
$temperature = $day->temperature;
typ__1();
if ($hot == true) {
$bottom = "Shorts";
} else if ($hot == false) {
$bottom = "Pants";
}
<div><?php echo $bottom ?></div>
<?php endforeach ?>
So the main issue/question is if I'm using the function correctly. Can I write if-statements in an outside function and then use them inside a
foreach-loop? The reason/goal is to shorten the foreach-loop.
(This is a reduced example, so there could be a typo somewhere in there.)
Thanks for your help!
Everything is about scope of PHP variables. You should "inject" variable into function like this:
<div>some HTML here</div>
<?php
function typ__1($temperature) {
if ($temperature >= 29) {
return true;
}
return false;
}
?>
<?php foreach (array_slice($data->something->something, 0, 5) as $day):
if (typ__1($day->temperature)) {
$bottom = "Shorts";
} else if (typ__1($day->temperature)) {
$bottom = "Pants";
}
<div><?php echo $bottom ?></div>
<?php endforeach ?>
http://php.net/manual/en/language.variables.scope.php
Add parameter in your function and return a value.
<?php
function typ__1($temperature) {
if ($temperature >= 29) {
$hot = true;
} else {
$hot = false;
}
return $hot;
}
?>
<?php foreach (array_slice($data->something->something, 0, 5) as $day):
$temperature = $day->temperature;
$hot=typ__1($temperature);
if ($hot == true) {
$bottom = "Shorts";
} else if ($hot == false) {
$bottom = "Pants";
}
<div><?php echo $bottom ?></div>
<?php endforeach ?>
I have a condition in which it is using a variable to pull either a number through 0-17, the string "MAKEUP", or the variable will be empty. I would like it to output the text "WIN" if the variable is greater than the number 8 and "LOSS" if the variable is less than the number 9. I would also like it to out "MAKEUP" if the variable consist of the string MAKEUP, and to display nothing if the variable is empty. Seems pretty simple to me, but I'm having issues particularly with the empty part. Can anyone let me know what I am doing wrong here? Code below
<?php
$t1w8 = '';
$result = $t1w8;
if ($result > 8 && !empty($result)) {
echo 'WON';
} elseif ($result < 9 && !empty($result)) {
echo 'LOSS';
} elseif ($result == 'MAKEUP') {
echo '-';
} else {
echo 'yooo';
}
?>
Make some changes in your conditions like this
<?php
//$result = "MAKEUP";
$result = 0;
if ($result === 'MAKEUP') {
echo '-';
}else if (is_numeric($result) && $result < 9 ) {
echo 'LOSS';
}else if (is_numeric($result) && $result >= 9 ) {
echo 'WON';
} else{
echo 'yooo';
}
?>
Live demo : https://eval.in/897120
try with this code
<?php
//$result = "MAKEUP";
$result = "";
//$result = "9";
//$result = "-";
if ($result == 'MAKEUP' && !empty($result) ) {
echo '-';
} elseif ($result > 8 && !empty($result)) {
echo 'WON';
} elseif ($result <= 8 && !empty($result)) {
echo 'LOSS';
} else {
echo 'yooo';
}
?>
for demo :demo code here
You have explained that your number range is from 0-17.
You have also explained that you could get the word MAKEUP.
Based upon those constraints we could use something like this
$output = "";
// Do we have something?
if(strlen($result) > 0) {
if (strtolower($result) == "makeup") {
$output = "MAKEUP";
}
// assumes a single digit string
else if ($result < 9) {
$output = "LOSS";
} else if ($result <= 17) {
$output = "WIN";
}
}
echo $output;
im quite new to PHP and im trying to make a pagination, the code works! but i need to click on the button twice to make the page refreshed/change. how do i fix this?
view/main/user/userlist.php
<?php
$_SESSION['pagelim'] = 10;
$_SESSION['page'] = $_GET['halaman'];
if ($_SESSION['page'] == '') {
$_SESSION['pos'] = 0;
$_SESSION['page'] = 1;
} else {
$_SESSION['pos'] = ($_SESSION['page']- 1) * $_SESSION['pagelim'];
}
$itemcount = listpetugas::getlisted();
$pagecount = ceil(listpetugas::getlisted()/$_SESSION['pagelim']);
for ($i=1;$i<=$pagecount;$i++)
{ ?>
<?php
if ($i!=$pagecount){
echo " <ul class='pagination'><li><a href='?controller=main&action=userlist&halaman=$i' onclick='myFunction()'>$i</a></li></ul>";?>
<?php }
else{
echo "$i";
} ?>
<?php } ?>
model/userdb.php
public static function listemup()
{
if ($_SESSION['pos'] =='' && $_SESSION['pagelim'])
{
$pos = 0;
$lim = 10;
}
else {
$pos = $_SESSION['pos'];
$lim = $_SESSION['pagelim'];
}
$list = [];
$db = FirstFire::StartConnection();
$req = $db->query("SELECT * FROM randomity LIMIT $pos,$lim");
$rowcount = $req->rowCount();
foreach ($req->fetchAll() as $post) {
$list[] = new listpetugas($post['name'],$post['email'],$post['adress'],$post['wow']);
}
return $list;
}
JS
<script>
function myFunction() {
location.reload();
}
</script>
sorry for the messy code.