Print specific array value after query in php yii2 - mysql

In my yii2 project I wrote query:
$Result = $query->select(['LT.Title','L.Balance', 'L.Earned'])->where(['L.EmployeeID'=>Yii::$app->user->id])->from('leave L')->leftJoin('listitems LT','LT.ListItemID = L.LeaveTypeID')->all();
and get result:
Array
(
[0] => Array
(
[Title] => sick
[Balance] => 12
[Earned] => 12
)
[1] => Array
(
[Title] => casual
[Balance] => 12
[Earned] => 12
)
)
the result is as I expected;
How can I print a single value like Title, Balance, Earned?
I tried using foreach loop but I am unable to get the result and get error Trying to get property of non-object instead.

<?php foreach ($Result as $key => $Res) {
echo "<div class='col-lg-4'>
<div class='well'>".$Res['Title']."
<p>".$Res['Earned']."</p>
<p>".$Res['Balance']."</p>
</div>
</div>";
}
?>
Doing This simply i am able to get what i wanted to .

Related

Add/update and display an array of meta_values

I am trying to add an array of meta_values to a single meta_key and display them in a list like so:
Assessment One, Passed
Assesment Two, Passed
My code for updating/adding the user_meta:
$user_id = $current_user->ID;
$key = 'assessment_result';
$assessment_name = get_the_title();
$new_value = array($assessment_name, 'Passed');
$meta = get_user_meta($user_id, $key, false);
if ( ! array($meta) ) {
$meta = array();
}
$meta[] = $new_value;
update_user_meta($user_id, $key, $meta, $unique);
After completing two assessments, my array looks like this:
Array ( [0] => Array ( [0] => Array ( [0] => Array ( [0] => Assessment Two [1] => Passed ) ) [1] => Array ( [0] => Assessment One [1] => Passed ) ) ) 1
There seems to be too many arrays!
Then my query looks like this:
$user_assessment_results = get_user_meta($user_id, 'assessment_result', true);
echo '<ul>';
foreach ($user_assessment_results as $user_assessment_result) {
echo '<li>';
$output = array();
foreach ($user_assessment_result as $result_item) {
$output[] = $result_item;
}
if( count( $output ) ) echo join( ", ", $output );
echo '</li>';
}
echo '</ul>';
Which results in:
Array
Assessment One, Passed
Can you suggest where I am going wrong?
I found the problem $meta = get_user_meta($user_id, $key, false); should have been $meta = get_user_meta($user_id, $key, true);

Opencart retriving data from 2 tables

I´m trying to join 2 tables in opencart to make an sales report that specifies taxes (by class, there can be many tax%) for every order. I can only get the values from 1 table, but not 2.
How should I do this?
<?php $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order`, `" . DB_PREFIX . "order_total`");
$ordersr = array();
// Check if there are any rows returned from the query
if ($query->num_rows > 0) {
// Loop through the returned rows for processing
foreach ($query->rows as $result) {
$ordersr[] = array(
'order_id' => $result['order_id'],
'invoice_no' => $result['invoice_no'],
'total' => $result['total'],
'order_status_id' => $result['order_status_id'],
'date_added' => $result['date_added'],
'date_modified' => $result['date_modified'],
'firstname' => $result['firstname'],
'lastname' => $result['lastname'],
'payment_country' => $result['payment_country'],
'payment_method' => $result['payment_method'],
'payment_code' => $result['payment_code'],
'shipping_method' => $result['shipping_method'],
'shipping_code' => $result['shipping_code'],
'currency_code' => $result['currency_code'],
'currency_value' => $result['currency_value'],
'order_status_id' => $result['order_status_id'],
'date_payed' => $result['date_payed']
);
}
} ?>

Implode array -> check characters -> inset to Database

I got a Array like this....
Array ( [register] => Array ( [0] => Array ( [firstname] => Marc [surname] => hamster [company] => 12345678 [position] => !\"§$%&/(hamste$r ) [1] => Array ( [country] => [zip] => [city] => [street] => ) [2] => Array ( [homepage] => [phone] => [mobile] => ) [3] => Array ( [email] => [password1] => [password2] => ) ) )
I put these in a Database like this:
// build query...
$sql = "INSERT INTO table";
// implode keys of $array...
$sql .= " (`".implode("`, `", array_keys($array))."`)";
// implode values of $array...
$sql .= " VALUES ('".implode("', '", $array)."') ";
// execute query...
$result = mysql_query($sql) or die(mysql_error());
But before i want to replace special Characters like this....to avoid SQL inject
$text= preg_replace("/[^a-zA-Z0-9\-öäüÖÄÜ# ]/","",$text);
Is there a simple and smart way to do it ?
I think you will need to loop through the array first* - for example:
foreach ( $columns as $value){
$value = preg_replace etc...;
}
Then run the query.
*before you build the query I guess.

Mysql returns only the first result

I have this query for a search form:
// General Query
$conditions = array(
'editore LIKE' => "%$e%",
'titolo LIKE' => "%$t%"
);
Now, if the user chooses fill some field (eg author, year, etc), I have:
if (isset($menu)&&$menu!='')
$conditions[]=array('classe LIKE' => "%$menu%");
Or:
if ($anno&&$anno2)
$conditions[] = array('anno BETWEEN ? AND ?' => array($anno,$anno2));
If the user chooses some tags for the book, I have:
$query_t = $CdBibliotem->query("SELECT codiceBiblio FROM cd_bibliotem WHERE codiceTematica IN (".implode(',', $fields).")");
$query_t = Set::classicExtract($query_t,'{n}.cd_bibliotem.codiceBiblio');
$tot=implode(',', $query_t);
$conditions[]=array('codiceBiblio IN (?)'=> "$tot");
This should work, but instead I don't know why, it gives me only the first record.
This is an example of debug a final query, where the user selects only 1 tag, and leaves the others fields empty:
Array (
[editore LIKE] => %%
[titolo LIKE] => %%
[0] => Array
(
[autori LIKE] => %%
)
[1] => Array
(
[codiceBiblio IN (?)] => 118729,118656,118645,118554,118534,118533,118532,118531,118530,118529,118528,118527,118526,118121,117632,117515,117040,116562,115851,115787,114613,114612,113545,113385,113142
) )
In this case, it gives me all the details of the first book (with id=118729), while it should gives me the details of all that books.

Joomla: two mysql queries model functions with one view

I am trying to run two model functions from one view. The first query appears to be working fine.
The second query returns a blank array. I noticed when printing the query to the screen that the queries appear to be joining together and are not separate, like this:
JDatabaseQueryMySQL Object ( [db:protected] => JDatabaseMySQL Object ( [name] => mysql [nameQuote:protected] => ` [nullDate:protected] => 0000-00-00 00:00:00 [dbMinimum:protected] => 5.0.4 [_database:JDatabase:private] => Curling_Schedule [connection:protected] => Resource id #19 [count:protected] => 0 [cursor:protected] => Resource id #72 [debug:protected] => [limit:protected] => 0 [log:protected] => Array ( ) [offset:protected] => 0 [sql:protected] => SELECT team_name, division FROM #_autoschedTeams ORDER BY division [tablePrefix:protected] => encex [utf:protected] => 1 [errorNum:protected] => 0 [errorMsg:protected] => [hasQuoted:protected] => [quoted:protected] => Array ( ) ) [type:protected] => select [element:protected] => [select:protected] => JDatabaseQueryElement Object ( [name:protected] => SELECT [elements:protected] => Array ( [0] => sheet, id ) [glue:protected] => , ) [delete:protected] => [update:protected] => [insert:protected] => [from:protected] => JDatabaseQueryElement Object ( [name:protected] => FROM [elements:protected] => Array ( [0] => #__autoschedPlayingSheets ) [glue:protected] => , ) [join:protected] => [set:protected] => [where:protected] => [group:protected] => [having:protected] => [columns:protected] => [values:protected] => [order:protected] => JDatabaseQueryElement Object ( [name:protected] => ORDER BY [elements:protected] => Array ( [0] => sheet ) [glue:protected] => , ) [union:protected] => [autoIncrementField:protected] => )
Model:
public function getTeams()
{
// Create a new query object.
$db = JFactory::getDBO();
$query = $db->getQuery(true);
// Select some fields
$query->select('team_name, division');
// From the hello table
$query->from('#__autoschedTeams');
$query->order('division');
$db->setQuery((string)$query);
$teams = $db->loadResultArray();
$div = $db->loadResultArray(1);
$divs = array_unique($div);
$result = [];
foreach($divs as $key)
{
$result[$key] = [];
foreach(array_keys($div, $key) as $index)
{
array_push($result[$key], $teams[$index]);
}
}
return $result;
}
public function getSheets()
{
// Create a new query object.
$db = JFactory::getDBO();
print_r($query);
$query = $db->getQuery(true);
// Select some fields
$query->select('sheet, id');
// From the hello table
$query->from('#__autoschedPlayingSheets');
$query->order('sheet');
//print_r($query);
$db->setQuery($query);
$result = $db->loadResultArray();
return $result;
}
}
relevant code from View:
$teams_sched = $this->get('Teams');
$sheets_sched = $this->get('Sheets');
Change the name of the variable and check with it.
If you are not getting the result again, then print the query echo $query; and check with it.
public function getSheets()
{
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('sheet, id');
$query->from('#__autoschedPlayingSheets');
$query->order('sheet');
$db->setQuery($query);
$Sheets = $db->loadResultArray();
print_r($Sheets);
return $Sheets;
}