I am using Pear PHP and HTML quickforms to create a dropdown, concatenate results and maintain them in a drop down.
Code that fetches the data and concatenates:
DO_Common::DebugLevel(1);
$stDO = DO_Common::factory('SoftwareTypes');
$lmDO = DO_Common::factory('LicenseMethods');
$tDO = DO_Common::factory('sldSoftwareType');
$stDO->selectAdd();
$stDO->selectAdd('Title, Method, sldSoftwareType.Type, SoftwareTypes.ID');
$stDO->joinAdd($lmDO);
$stDO->joinAdd($tDO);
$stDO->whereAdd("SoftwareTypes.Flag <> 1");
$stDO->find();
$lmst = array();
while ($stDO->fetch())
{
$lmst[] =$stDO->ID . "| " . $stDO->Title . " | " . $stDO->Method . " | " .$stDO->Type ;
// $text = array_column($lmst[], $stDO->ID);
}
Dropdown creation code:
$ddlSoftwareType = $form->addElement('select', 'ddlSoftwareType', 'Software Type', $lmst, array('id' => "SoftwareTypeList", 'orderBy' => "Type", "OnChange" => "GetDetails();"));
Although this works perfectly I want to be able to add the ID of the original table as the value of the dropdown.
Is this possible?
Related
I need to add a UNIQUE ID and a string that is with that ID , into an array. This array needs to be updated too. Example:
ID NAME[(3,Flor)(5,Dries)] Then I need to have a check when I want to insert a new value in this array if the ID already is in the array or not. I currently have this:
$werknemers= [];
while($werknemer= $werknemersql->fetch_assoc()){
$werknemerid = $werknemer['userid'];
$naam = $werknemer['name'] ." " . $werknemer['familyname'];
array_push($werknemers, array("id" => $werknemerid, "naam" => $naam ));
}
echo $werknemers[1][1];
A simple technique to achieve that is to always update the array, with a consistent index:
$werknemers = [];
while($werknemer = $werknemersql->fetch_assoc()){
$werknemerid = $werknemer['userid'];
$naam = $werknemer['name'] . " " . $werknemer['familyname'];
$index = $werknemerid . '.' . $naam;
$werknemers[$index] = ["id" => $werknemerid, "naam" => $naam];
}
And then, if necessary, get rid of the indexes:
$werknemers = array_values($werknemers);
$werknemers= [];
while($werknemer= $werknemersql->fetch_assoc()){
$werknemers[$werknemer['userid']] = $werknemer['name'] . " " . $werknemer['familyname'];
}
Use id as index and it's solved :)
Hi I am currently using a PostCode Anywhere API subscription to fetch addresses based on a post code, step 1, is to get a range of addresses based on the post code, then step 2, is to save that ID and full address details in a separate table. At the moment I have this working using the following code:
global $strTableName;
$postcode= $params["value1"];
//Set Post Code Variables
$Key = $_SESSION["PAFKey"];
$SearchTerm = $postcode;
//Build URL Request
$url = "http://services.postcodeanywhere.co.uk/PostcodeAnywhere/Interactive/Find/v1.10/xmla.ws?";
$url .= "&Key=" . urlencode($Key);
$url .= "&SearchTerm=" . urlencode($SearchTerm);
//Make the request to Postcode Anywhere and parse the XML returned
$file = simplexml_load_file($url);
//Check for an error, if there is one then throw an exception
if ($file->Columns->Column->attributes()->Name == "Error")
{
throw new Exception("[ID] " . $file->Rows->Row->attributes()->Error . " [DESCRIPTION] " . $file->Rows->Row->attributes()->Description . " [CAUSE] " . $file->Rows->Row->attributes()->Cause . " [RESOLUTION] " . $file->Rows->Row->attributes()->Resolution);
}
//Copy the data
if ( !empty($file->Rows) )
{
foreach ($file->Rows->Row as $item)
{
$Data[] = array('Id'=>$item->attributes()->Id,'StreetAddress'=>$item->attributes()->StreetAddress,'Place'=>$item->attributes()->Place);
$newStreetAddress = str_replace("'", "", $item["StreetAddress"]);
$newPlace = str_replace("'", "", $item["Place"]);
$sql = "insert into Global_temp_address (id, StreetAddress, Place, PostCode, AddedBy) values ('".$item["Id"]."', '$newStreetAddress', '$newPlace','$SearchTerm', '".$_SESSION["user"]."')"; CustomQuery($sql);
}}
This does work, but I have only been able to do so by creating a temporary table, then using a two part form (2nd part is then used to select address on dropdown, after selecting this, the temp table is then wiped).
This isnt the best process, I would like to know how to get the returned data and display this in a popup dropdown box or something similar? Any other suggestions are welcome
I am trying to write data from MySQL to PHP-Array (to generate a XML file with the PHP library FluidXML ). When I echo the array, I only get the first value of it, but when I echo a variable with the same data as the array I get correct output with all the information. Let me explain more exactly:
The query to get data:
$sql = sprintf("select b.nPosID, b.nAmount, b.sName, .......");
$result = mysql_query($sql);
Then I loop trough the results:
$msg = "";
$orderArticles = [];
$orderSubArticles = [];
while($data = mysql_fetch_array($result))
{
if($data['sChange'] != null) {
$msg .= ' * ' . $data['sChange'] . ' ' . number_format($data['nAmount'] * $data['nPriceChange'], 2) . "\r\n";
$orderSubArticles[] = ['SubArticle' => [
'ArticleNo' => '0',
'ArticleName' => $data['sChange'],
'Count' => $data['nAmount'],
'Price' => $data['nPriceChange']
],];
}
if ($nPosID != $data['nPosID']) {
$msg .= " \r\n" . $data['nAmount'] . ' x ' . $data['sName'] . ' ' . number_format($data['nAmount'] * $data['nPrice'], 2) . "\r\n";
$orderArticles[] = ['Article' => [
'ArticleNo' => '0',
'ArticleName' => $data['sName'],
'ArticleSize' => '0',
'Count' => $data['nAmount'],
'Price' => $data['nPrice'],
'Tax' => '10',
'SubArticleList' => [
$orderSubArticles
]],];
}
}
Let's assume, from the SQL query I get the following correct output:
Pizza
+ extra cheese
+ extra tonno
When I echo $msg variable, I get the same correct result. But when I echo the array, I only get the first value:
Pizza
+ extra cheese
To be exactly, the output which was generated with the values from the array:
<ArticleList>
<Article>
<ArticleName>Pizza</ArticleName>
<Count>1</Count>
<Price>12.9</Price>
<SubArticleList>
<SubArticle>
<ArticleName>Extra cheese</ArticleName>
<Count>1</Count>
<Price>3</Price>
</SubArticle>
</SubArticleList>
</Article>
</ArticleList>
So the second <SubArticle> is missing (extra tonno).
Without knowing how your resultset really looks like, I am doubting your assumption is correct, that a result/row in the set really contains both subarticles. That would lastly mean all results have two subarticles and that's supposingly not the case, because then it would need to return something like sChange1, sChange2 ...
Even if the results would look something like that and contain two or more sub articles, your code only assigns one of them to the subArticles-array per result.
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;
}
}
I'm trying to make a left join select and use AND to compare a column against a specific value, i.e. a condition.
The problem is I don't know where to add the condition. The below medoo query returns the first shirt id of the same user id even if all 'is_wearing_shirt' set to 0. A regular mysql query however returns the expected data.
Here's the medoo query:
$db->select('users', array(
'[>]shirts' => array(
'user_id' => 'shirt_owner_id',
'is_wearing_shirt' => 1 // equivalent to AND is_wearing_shirt = 1
)
), array(
'user_id',
'shirt_id(shirt_id_being_worn)'
) , array(
'user_id' => 1,
'LIMIT' => 1
));
// always returns a shirt_id (even if all rows have is_wearing_shirt set to 0, in which case, shirt_id should be null)
Here's the regular MySQL query that works:
SELECT u.user_id, s.shirt_id
FROM `cvs_users` u
LEFT JOIN `shirts` s
ON user_id = shirt_owner_id
AND shirt_being_worn = 1
WHERE user_id = 1
//returns the correct shirt_id
Concatenation of logical expressions using AND/OR in join clause is currently not supported by the medoo library (medoo 0.9.6.2). You can only use the query($query) method given by medoo to directly execute sql queries. For your example the query looks like this:
$data = $db->query("SELECT u.user_id, s.shirt_id FROM `cvs_users` u
LEFT JOIN `shirts` s
ON user_id = shirt_owner_id AND shirt_being_worn = 1
WHERE user_id = 1")->fetchAll();
Note the call to fetchAll() at the end to get the queried data.
I ran into the same problem and debugging the medoo.php code revealed that AND/OR conditions are only considered within where clause. Maybe they'll put this feature in a future update. I have started an issue describing the problem: Medoo Issue
I submitted a pull request that tries to fix this: https://github.com/catfan/Medoo/pull/206
What I did was to change medoo.php lines 564-568
// For ['column1' => 'column2']
else
{
$relation = 'ON ' . $table . '."' . key($relation) . '" = "' . (isset($match[5]) ? $match[5] : $match[3]) . '"."' . current($relation) . '"';
}
to
// For ['column1' => 'column2']
else
{
$and_relation = 'ON ' . $table . '."' . key($relation) . '" = "' . (isset($match[5]) ? $match[5] : $match[3]) . '"."' . current($relation) . '"';
// if there is more than one condition
if (count($relation) > 1) {
// remove the "ON" part
array_shift($relation);
// add the condition to the join
foreach ($relation as $key => $value) {
// if the [$] value modifier is present
if (preg_match('/\[\${1}\]/', $key) === 1) {
$key = preg_replace('/\[\${1}\]/', '', $key);
$and_relation .= ' AND "' . (isset($match[5]) ? $match[5] : $match[3]) . '"."' . $key . '" = ' . $value;
continue;
}
// add additional table_relation
$and_relation .= ' AND ' . $table . '."' . $key . '" = "' . (isset($match[5]) ? $match[5] : $match[3]) . '"."' . $value . '"';
}
}
// write our new relation back
$relation = $and_relation;
}
Note:
If the addition is a value with the joined table use the modifier [$]: 'right_table_column[$]' => value. If the value is a string you'll have to quote it before passing it the the join array, e.g.
// $db = Medoo Object
$value = $db->quote('myString');
$db->select(['table',
[
'[>]right_table' => [
'join_condition' => 'join_condition', // ON
'additional_condition[$]' => $value, // AND
],
],
[
...where conditions...
]
]);
If it is a table relation use it as any other join condition 'left_table_column' => 'right_table_column'