I'm modifying some code but I'm not an expert on php level and need some help :)
<div class="emd-main emd-main-<?php echo $emd_state; ?>">
<?php if ( $userpro->memberlist_in_search_mode($args) ) { ?>
<?php $arr = $userpro_emd->users( $args );
if (isset($arr['users']) && !empty($arr['users']) ) {
?>
<?php if (isset($arr['paginate']) && $args['emd_paginate'] && $args['emd_paginate_top'] == 1) { ?>
<div class="userpro-paginate top"><?php echo $arr['paginate']; ?></div>
<?php } ?>
<div class="emd-list" data-layoutmode="<?php echo $args['emd_layout']; ?>">
<?php foreach($arr['users'] as $user) { $user_id = $user->ID; ?>
<?php $tk_image_1 = get_field('foto_1', 'user_'. $user_id); ?>
<?php if (!empty($tk_image_1)) { ?>
<div class="emd-user">
I have the following issue; I have a page that shows a grid with members. The number of members per page is 20 (that is set within the plugin settings). The foreach is starting with this:
<?php foreach($arr['users'] as $user) { $user_id = $user->ID; ?>
After that I check if the value $tk_image_1 is not empty, if not empty then go on. So far so good.
The only thing now is; when I have 20 members on a page and for 8 of them the $tk_image_1 is empty then it shows 12 members on that page... I think it's something in the array that counts them before checking the $tk_image_1 value.
What is need is to show 20 members per page and only the one if $tk_image_1 is not empty.
Can someone help me with this?
Many thanks!
Regards,
Robert
The source of your problem is that you're working with a set of results that contains users you don't want to show. To properly correct this, you need to alter the query that's taking place in this code fragment: $userpro_emd->users( $args )
That ->users() method may accept additional $args that would allow you to say, "only users who have foto_1". That way you're not trying to perform magic in the loop, and then be left with the problem of not having enough others to make up for those who were missing foto_1.
Related
I developing a Intranet and I'm a bit stuck with allowing access for individual users. All works fine when I limit the access to an element, if that element in at the bottom/last element. I need this to usable where ever I want. If you are in the Directors group, get the element. If you are in the All group only, you get nothing. Any help would be great.
The HTML:
.....
<?php include('admin/Directors.php');
echo 'foooooo':
?>
....
<?php include('admin/All.php');
echo 'baaaar':
?>
...
The PHP (Directors.php):
<?php
session_start();
$allowed_users = array('mark','joe','allan');
if(!in_array($_SESSION['user'],$allowed_users)) die('');
?>
From wat I understand is happening here is that its reading the Directors.php file and applying it to entire HTML file.
Try this In your Directors.php:
session_start();
$allowed_users = array('mark','joe','allan');
return in_array($_SESSION['user'],$allowed_users));
And this in your html:
$allowed = include('admin/Directors.php');
if($allowed)
{
echo 'foooooo';
}
Instead of killing script with die() simply return the evaluation value check it in your html. But if there is other stuff in Director.php you can do this.
Try this In your Directors.php:
session_start();
$allowed_users = array('mark','joe','allan');
$allowed =in_array($_SESSION['user'],$allowed_users));
And this in your html:
include('admin/Directors.php');
if($allowed)
{
echo 'foooooo';
}
Solved this with edited code below. Thanks to all who helped!
I have two records in my db. Each record has 6 fields (challengeId, partnerName, code, challengeTitle, description, image_url). I select a given partnerName from my parent page view to go to my child page view.
I was using a foreach loop and having problems. I have now taken out my foreach loop and replaced it with <?php $challengename = $this->challengenames[$k] = current($this->challengenames); ?> but now cannot get the child page to display the values for challengeTitle that correspond to 'partnerName' I chose on my parent page. It always provides the challengeTitle value of the first record instead of the current record. I need to know how to make the challengeTitle value change depending on which partnerName I chose on my parent page.
Will making this a do while loop or if statement in the child page controller fix this?
Any advice (and code changes) is very much appreciated.
parent page controller
public function viewChallengesAction(){
//get instance for request
$request = JO_Request::getInstance();
//get activated challenge names and set variables
$myChallenge=$this->getChallenge();
$this->view->challengenames = array();
foreach($myChallenge AS $k=>$challengename){
$this->view->challengenames[$k]['href'] = WM_Router::create($request->getBaseUrl() . '?module=challenges&controller=index&action=yourChallenge?code=' . $challengename['partnerName']);
$this->view->challengenames[$k]['partnerName'] = $challengename['partnerName'];
}
CHILD VIEW
<div id="defaultcontainerwrapper" class="maxwidth">
<?php $challengename = $this->challenge; ?>
<header>
<h1>
<div class="list">
<span>Welcome to </span><?php echo $challengename['partnerName']; ?><span>'s Beat Waste Challenge!</span>
</div>
</h1>
</header>
<?php } ?>
</div>
CHILD CONTROLLER
public function yourChallengeAction(){
//get activated challenge names and set variables
$request = JO_Request::getInstance();
$myChallenge=$this->getChallenge();
$code = $request->getQuery("code");
$this->view->challengenames = array();
foreach($myChallenge AS $k=>$challengename){
if ($challengename['partnerName'] == $code)
{
$this->view->challenge = $challengename;
break;
}
}
If you don't know the key and want the current element of the array, you could use:
$challengename = current($this->challengenames);
Or the first element of the array:
$challengename = reset($this->challengenames);
Or the last element of the array:
$challengename = end($this->challengenames);
Just note that end and reset will change the pointer location of the array.
<?php $challengename = $this->challengenames[0] ?>
You are missing a last ; try add it agaign;
try below:
<?php $challengename = $this->challengenames[0]; ?>
I'm trying to code the last 5 page names the user viewed on my site and produce it into a list. I'm currently able to get the current page name, but I don't know how to get the previous pages. This is the code I'm using to get the current page name:
<?php
$pageName = basename($_SERVER['PHP_SELF']);
echo $pageName;
?>
PHP Sessions should get you going in the right direction. For example:
session_start();
if(!isset($_SESSION['pages'])) {
$_SESSION['pages'] = array();
}
if(count($_SESSION['pages']) < 5) {
$_SESSION['pages'] [] = $_SERVER['PHP_SELF'];
} else {
echo "Limit reached";
}
print_r($_SESSION['pages']);
I recommend you use PHP Sessions to accomplish this.
So, save the current page name that you want to the sessions variable like so:
<?php
$pageName = basename($_SERVER['PHP_SELF']);
$_SESSION['pageName'] = $pageName;
?>
And then continue to save these names. #Len_D just beat me to the punch with an answer that uses arrays and is likely what you need.
I researched here in stackoverflow trying to find whether someone is also encountering the same problem. I know it's kind of easy and even I really don't know what's the error because there's no problem with my query.
On the previous page, here's my code to retrieve the ID Number so I'll be able to select the data with that ID number:
<?php echo $row['place_name'];?>
I tried first to print the value of the place id and it works fine.
But when it was being called to the Package page, the data I want to show weren't displayed.
I look at the URL and it shows this after the package.php
place_id=
I don't know why it is blank, please check my code if there's missing or just wrong.
In my package page, here's the PHP code:
<?php
include("common/connect.php");
$place_id = $_GET['place_id'];
$result = mysql_query("SELECT * FROM package_items WHERE place_id = '$place_id'");
$row1 = mysql_fetch_array(mysql_query("SELECT place_name FROM packages WHERE place_id = '$place_id'"));
if($result === FALSE) {
die(mysql_error()); // for better error handling
}
?>
In HTML Code:
<h1><?php echo $row1['place_name'];?></h1>
<?php while($row=mysql_fetch_array($result)) {?>
<?php echo $row['item_title'];?>
<br>
Back
<?php } ?>
Please check my codes. Thanks.
You are not printing it.
Change
<?php $row['place_id'];?> // It will output nothing as no echo or print.
To
<?php echo $row['place_id'];?>
Rest of the code looks fine.
Three suggestions:
1)
$place_id = $_GET['place_id'];
Change to
$place_id = ! empty($_GET['place_id']) ? $_GET['place_id'] : ''; // To avoid any warning.
2) Don't feed variable from $_GET or $_POST to any SQL.
3) Don't use mysql_ functions as they are deprecated and will be remove in future versions of PHP.
I have a site based on wordpress. I need to allow people to create posts from frontend so I have made a multi-part form which works pretty well. There are three parts of the form and each part of the form is validated before moving to the next part. Data is passed to another page through hidden inputs.
My form template looks somewhat like this ( complete code is pretty massive and irrelevant here, so just showing just the relevant parts ) which I hope is enough to give an idea how the form works.
MULTI-PART FORM WP-TEMPLATE SAMPLE CODE :
<?php
global $wpdb;
$this_page = $_SERVER['REQUEST_URI'];
$page = $_POST['page'];
if ( $page == NULL ) { ?>
<?php include_once('multiparts/form-files/first_part.php'); ?>
<?php } else if ( $page == 1 ) { ?>
<?php include_once('multiparts/validation/validate_first_part.php');
if (isset($_POST['submit-1']) && (!empty($error))) { ?>
<div class="error-head">Cannot continue registration. Error/s highlighted below.</div><br/>
<?php echo $error . '</br>'; ?>
<?php } else {
include_once('multiparts/form-files/second_part.php');
}
?>
<?php
} else if ( $page == 2 ) { ?>
//SO ON AND SO FORTH
<?php
}
?>
Recently, I have a added several checkbox fields in the form with an intention to display values of selected checkboxes, in the created posts. So here is a relevant html form code that I am currently using.
<fieldset class="work-areas">
<label for="areas" class="label">INTERESTED IN :</label></br>
<div class="work-class">
<input type="checkbox" name="workareas[]" value="administration"/>administration</br>
<input type="checkbox" name="workareas[]" value="technical"/>technical</br>
<input type="checkbox" name="workareas[]" value="creative"/>creative</br>
<input type="checkbox" name="workareas[]" value="fieldwork"/>fieldwork</br>
<input type="checkbox" name="workareas[]" value="marketing"/>marketing</br>
</div>
</fieldset>
I insert the values of these checkboxes into the post just like any other custom fields using this code: add_post_meta($pid, 'areas', $workareas, true );. In the processing part it is assigned a meta_key areas. I display it in the single.php with the code below :
<?php $areas = get_post_meta($post->ID, 'areas', true); ?>
<?php if (is_array($areas)) : ?>
<h4>INTERESTED AREAS OF WORK:</h4>
<?php if (is_array($areas)) {
foreach($areas as $area) {
echo '<li>'.$area.'</li>';
}
}
?>
<?php endif;?>
ISSUE: All this works well when the above given html form code for checkboxes is in the last/third part of the form. But it does work when the same checkbox fields is in the second part of the form. I guess it simply does not pass the array values of the selected checkboxes to the third part. Print_r shows an empty array and obviously does not display anything in the single.php too. Although I understand that the trouble is just the array of selected checkbox values, NOT being carried to the third part properly, I need help as I am noob to all this.
So the bottomline question is how do I save the array of the selected
checkboxes' values in the second part and carry it to the third part
and finally assign it to a variable which will hold the array values.
That which can be displayed in post using the code above.
THINGS TRIED : I have looked into this thread here and I am confused where I will insert my checkbox fields and not even sure it applies to my situation. I have been able to pass other text input values from one part to another part of the from using something like this :
<input type="hidden" name="eligible" value="<?php echo $eligible;?>" />
So, I tried using name="workareas[]" but did not work. I am doing print_r() for everything I am trying and till now have only been getting empty array. I am still going through tons of other threads looking for possible hints. In the meanwhile if you can help, that will be great. Thanks in advance.
UPDATE : Solved, please check the answer.
Not a WP user but your checkboxes are named "workareas" but you seem to refer to them as "areas" everywhere else.
Thanks for the suggestions in the comments but I have found a graceful and robust solution in my opinion, that is using sessions. I followed the basic idea from here which actually deals with a multipart form with sessions. Now I have the following code in my third/last part of the form, at the very beginning of the document. At this time please remember that the html checkbox fields as illustrated above in the question are in the second part.
<?php
session_start();
$_SESSION['workareas'] = $_POST['workareas'];
$result=$_POST['workareas'];
?>
The code is that is repeated during the validation of the third part is the same but this way the variable $result is still holding the values of the array of the selected checkboxes from the second part of the form.
session_start();
$result=$_SESSION['workareas'];
You can do a print_r($result) at this point and check. Furthermore, if you want to insert these array values to post in order for them to show up in the post just assign meta_key eg. areas to $result and use the code below to add it as a custom field :
add_post_meta($pid, 'areas', $result, true);
In the single.php you can use the code below to pull values from the array and show. Do not avoid if(is_array) statement else wordpress might throw an error warning: invalid arguments supplied foreach(). Goodluck.
<?php $areas = get_post_meta($post->ID, 'areas', true); ?>
<?php if (is_array($areas)) : ?>
<h4>INTERESTED AREAS OF WORK:</h4>
<?php if (is_array($areas)) {
foreach($areas as $area) {
echo '<li>'.$area.'</li>';
}
}
?>
<?php endif;?>