A logged in user fills out a form several times. From his entries, I'm attempting to get all of his inputs for a specific field and put them into a PHP array.
For the sake of simplicity, assume the form has ID 10 with the first field called 'SomeField' and the user was logged in (and is still logged in) for all entries.
Here's my best attempt at creating an array of all SomeField entries from the user:
get_currentuserinfo();
$searchCriteria = array(
array(
'key' => 'created_by',
'value' => $current_user->user_login
),
array(
'key' => '1',
'value' => 'SomeField'
),
);
$form = GFAPI::get_entries( 10, $searchCriteria );
echo print_r($form);
Unfortunately, this print_r appears to display an empty array. I believe my searchCriteria is somehow incorrect.
I found it's easier to omit the second parameter ($searchCriteria) and simply use $form[0]['1'] for example which will display the first field of the first entry of the specified form.
I found with user23058230 great solution in the second answer I always got the latest form created, which worked well for new signups but not later on.
Assuming here that the form ID is number 1, you can search it with a for loop using the entry_id which is available in the user's login array as
$current_user->entry_id
No search query is needed other than the form you want returned - in this case form #1.
You can search other items using the keys you can see from your array dump.
Great solution which with the addition of this code solved my problem.
$form = GFAPI::get_entries( 1, '');
$what_i_want = 0;
for( $i = 0; $i < count($form); $i++ ){
if( $form[$i]['id'] == $current_user->entry_id ){
// the item I want
$what_i_want = $form[$i]['22'];
//echo " I " . $form[$i]['id'] . " F " . $form[$i]['22'] . " i " . $i . " T " . $what_i_want . '<br />';
break;
}
}
Related
I am creating a search module to show results from database, I am echoing out the data to check if I am receiving it from database.
here is my current output:
As you can see, the results are there but I wasn't able to display it on my table, and I also have that error for invalid argument for foreach(). Can anyone check what is the problem here?
echo form_open(site_url() . '/search/get_account',array('id' => 'formSearch'));
$data = array(
'name' => 'acctNum',
'id' => 'acctNum',
'type' => 'hidden',
'value' => set_value('acctNum',''),
);
$dataCertType = array($data);
$dataCertType[''] = '--';
if(! is_null($certType))
foreach($certType as $rowType)
$dataCertType[$rowType->certTypeId] = $rowType->certTypeName;
$formCertType = form_dropdown('certType', $dataCertType, set_value('certType'),'id="certType" class="dropdown"');
add brackets. echo out the form drop dropdown.
if(! is_null($certType)){
foreach($certType as $rowType){
$dataCertType[$rowType->certTypeId] = $rowType->certTypeName;
$formCertType = form_dropdown('certType', $dataCertType,set_value('certType'),'id="certType" class="dropdown"');
echo $formCertType ;
}
}
bonus points - do your is_null check in your controller, and then show an appropriate view.
I am trying to expand the functionality of a dictionary I am building by showing definitions for some of the terms (if a definition exists).
I take the data from the two tables as follows:
$query = $db->query("SELECT * FROM ".DICTIONARY_TABLE." " .
"LEFT JOIN ".DICTIONARY_DEFINITIONS." ON ".DICTIONARY_TABLE.".id = ".DICTIONARY_DEFINITIONS.".term_id ".
"WHERE ".DICTIONARY_TABLE.".".$source." LIKE '%".$keyword."%' ".
"AND ".DICTIONARY_TABLE.".theme_id = ".$selected_theme_id." ".
"ORDER BY ".DICTIONARY_TABLE.".id");
After that, in the while loop, I first get the theme name (from another table), then add the query results to an array:
while($row = $query->fetch(PDO::FETCH_ASSOC)) {
// get theme name
$theme_name = "theme_".$lang;
$theme_query= $db->query("SELECT theme_id,".$theme_name." FROM ".DICTIONARY_THEMES." WHERE theme_id = ".$theme_id."");
$theme_row = $theme_query->fetch(PDO::FETCH_ASSOC);
$theme = $theme_row[$theme_name];
// add all results to an array
$results[] = array(
'english' => $row['english'],
'bulgarian' => $row['bulgarian'],
'english_abbr' => $row['english_abbr'],
'bulgarian_abbr' => $row['bulgarian_abbr'],
'theme' => $theme
);
After that, I try to check if the LEFT JOIN has actually returned any results from the definitions table, and if yes, add those to the array as well - but this is where I fail...
// check if definition exists for this term
if(isset($row['bulgarian_definition'])) {
array_push($results['bulgarian_definition'], $row['bulgarian_definition']);
}
if(isset($row['english_definition'])) {
array_push($results['english_definition'], $row['english_definition']);
}
I've tried all ways I could find to first check if the variables have been defined, and then push them to the $results array. Nothing works.
I don't seem to be able to successfully find out of english_definition and/or bulgarian_definition are set. When I run the query itself in PhpMyAdmin, it works just fine.
The only "solution" I can think of is to scrap the idea of having a separate table for the definitions and just expand the main table, but that's not a great approach I know.
Any insight as to what I am doing wrong will be greatly appreciated. Thanks!
EDIT: I've changed the way elements are added to the array:
// check if definition exists for this term
if(isset($row['bulgarian_definition'])) {
$results['bulgarian_definition'] = $row['bulgarian_definition'];
}
if(isset($row['english_definition'])) {
$results['english_definition'] = $row['english_definition'];
}
And this now does the trick. When I dump the $results array outside of the while loop, both definitions have been added.
However, I now get a large number of Warning: Illegal string offset 'theme' in... and 'english' and 'bulgarian' - this happens below, when I run the $results array in a foreach loop to start printing them:
foreach($results as $result) {
if($theme != $result['theme']) {
$theme = $result['theme'];
$search_results .= "<h3>" . $result['theme'] . "</h3>";
}
if($source == "english") {
foreach ($keywords as $keyword) {
$result['english'] = preg_replace("|($keyword)|Ui", "<span style=\"color:#780223\">" . $keyword . "</span>", $result['english']);
}
No idea yet why this happens, will keep looking.
SECOND EDIT: Decided to put the two definitions directly inside the $results array as follows:
while($row = $query->fetch(PDO::FETCH_ASSOC)) {
// get theme name
$theme_name = "theme_".$lang;
$theme_query= $db->query("SELECT theme_id,".$theme_name." FROM ".DICTIONARY_THEMES." WHERE theme_id = ".$theme_id."");
$theme_row = $theme_query->fetch(PDO::FETCH_ASSOC);
$theme = $theme_row[$theme_name];
// add all results to an array
$results[] = array(
'english' => $row['english'],
'bulgarian' => $row['bulgarian'],
'english_abbr' => $row['english_abbr'],
'bulgarian_abbr' => $row['bulgarian_abbr'],
'theme' => $theme,
'bulgarian_definition' => $row['bulgarian_definition'],
'english_definition' => $row['english_definition']
);
}// end while
This now works just fine. When I dump the array, if no definition exists, I have 'english_definition' => null and if a definition exists, it's there. So far so good.
The new problem is that I can no longer group the results by theme - the theme of the last result found is shown. Which is a different problem altogether. What really irks me is that before I added the definitions, everything worked just fine. You can the working website here.
PROBLEM SOLVED!
Decided against pushing values to the array (as shown above).
Got rid of the extra query within the while loop that gets the theme's name, moving it instead to the query that performs the search. Thus the query itself is:
$query = $db->query("SELECT * FROM ".DICTIONARY_TABLE." " .
"JOIN ".DICTIONARY_THEMES." ON ".DICTIONARY_TABLE.".theme_id = ".DICTIONARY_THEMES.".theme_id ".
"LEFT JOIN ".DICTIONARY_DEFINITIONS." ON ".DICTIONARY_TABLE.".id = ".DICTIONARY_DEFINITIONS.".term_id ".
"WHERE ".DICTIONARY_TABLE.".".$source." LIKE '%".$keyword."%' ".
"ORDER BY ".DICTIONARY_TABLE.".theme_id, ".DICTIONARY_TABLE.".id");
And the while loop is:
while($row = $query->fetch(PDO::FETCH_ASSOC)) {
$theme_name = "theme_".$lang;
// add all results to an array
$results[] = array(
'english' => $row['english'],
'bulgarian' => $row['bulgarian'],
'english_abbr' => $row['english_abbr'],
'bulgarian_abbr' => $row['bulgarian_abbr'],
'theme' => $row[$theme_name],
'bulgarian_definition' => $row['bulgarian_definition'],
'english_definition' => $row['english_definition']
);
}// end while
The website link from above will now load the upgraded search functionality, where definitions will be shown (if they exist). One example word for anyone curious is "worm".
As it turns out, sometimes all it takes to fix a problem is to show it to people and then start thinking about it, as sometimes the solution is right in front of you. Thanks to all who participated!
EDITED: Deleted previous answer since it was incorrect. Will update this answer once I have a solution to the problem. Have not deleted this answer since I haven't found the option that does so. Though sometimes it is fun to take up space and feel important :), don't hate the player, hate the game.
hello all i am having a site where i need to show random users/ members in this format
i want images of the user in the format as above randomely i have more than 9000 members and need to show the random users in this format the design repeat itself by three
i dont know how to use the loop or use the query to show the images in the above format.
my query is like
$getimages=($con,"select image,id,somethingelse from tablename where id !='' order by rand() limit 21");
// i want 21 images to show here 7*3 (the image here repets it self by three)
while($theimagelink=mysqli_fetch_array($getimages)){
// i think array_push may help but dont know how to implement..
}
please give me some hints or any llink i am stuck over here .....
numbering of the image can be changed ....
i need all the three things of select query to display image
First, you want to get all members, and pull them into an array using your preferred method.
SELECT MemberID,ImageLink,Field4,Etc FROM Members
Then you can randomly select a MemberID by using array_rand():
<?php
$input = array(array('MemberID' => 1, 'ImageLink' => 'someimage.jpg'),array('MemberID' => 2, 'ImageLink' => 'someimage.jpg'),array('MemberID' => 3, 'ImageLink' => 'someimage.jpg'));
$rand_keys = array_rand($input, 2);
var_dump($input[$rand_keys[0]]) . "\n";
var_dump($input[$rand_keys[1]]) . "\n";
?>
Reference: http://php.net/manual/en/function.array-rand.php
To solve your updated question
$getimages=mysqli_query($con,"select image,id,somethingelse from tablename where id !='' order by rand() limit 21");
// i want 21 images to show here 7*3 (the image here repets it self by three)
$Results = array();
while($tmp=mysqli_fetch_assoc($getimages)){
$Results[] = array('image' => $tmp['image'], 'id' => $tmp['id'], 'somethingelse' => $tmp['somethingelse']);
}
var_dump($Results);
Then you can just iterate through the array
for ($i=0; $i < count($Results); $i++) {
echo "<img src='" . $Results[$i]['image'] . "' alt=''><br/>"
echo $Results[$i]['id'] . '<br/>'
echo $Results[$i]['somethingelse'] . '<br/><br/>'
}
I want to set the conditionally Order_BY in rest API
e.g("B" comes before "A"and "D" comes before "C" means the out put is appear like this "BADC")
please help me if it is possible
$get_entry_list_parameters = array(
//session id
'session' => $session_id,
//The name of the module from which to retrieve records
'module_name' => 'Accounts',
//The SQL WHERE clause without the word "where".
'query' => $query,
//The SQL ORDER BY clause without the phrase "order by".
'order_by' => " How to set Conditional Order By "
);
I guess BADC that you mentioned is column's name.
That should be done with some code like this:
<?php
$order_by = $_POST['order_by']; // order_by's format is like this: B:asc;A:asc;D:desc;C:desc;
$order_by_str = ""; // to store a query statement for 'order by'
$order_by_array = explode(';', $order_by);
foreach ($order_by_array as $order_item) {
$order_item_array = explode(':', $order_item);
$order_by_str .= "," . $order_item_array[0] . " " . $order_item_array[1];
}
$order_by_str = substr($order_by_str, 1); // result for order_by
This is a known bug.
There is a bug report with a proposed fix here Defect 66206: REST V4_1 API,function get_entry_list didn't work with the order_by attribute
Applying the proposed fix worked for me.
I'm working on a search system for my database where I break the search phrase into individual search words, search my mySQL database keyword table for any occurrence of those words, and then output a list of IDs associate with the keyword.
I want to add those ID's to a new array that will also contain a count value and (from a new query) the name of the place belonging to the ID, resulting in:
array(ID, count, name)
For each search word I want to go through this process and if the ID is already in the above array I want to increase the count value and if not, I want to add it with a count value of 1.
When the array is built and all the counting is done, I want to sort it by count and then name, and then output the results.
I've programmed PHP for quite some time but I've never been good with building and manipulating arrays so any help related to building, searching, editing, and sorting a 3-column array is appreciated.
Here's some code below where I'm just trying to insert data into the array and spit it out, which obviously doesn't work:
<?php
$id = 5;
$count = 1;
$name = "PlaceName1";
$arrayPlaces = array($id, $count, $name);
echo "5: " . $arrayPlaces[5] . "<br />";
?>
<?php
$id = 5;
$count = 1;
$name = "PlaceName1";
$arrayPlaces = array(
$id => array(
'count' => $count,
'name' => $name
)
);
echo "5: " . $arrayPlace[5]['name'] . "<br />";
?>