foreach php-smarty help - php

I have the php script as follows :
$sql = "
SELECT
*
FROM
tbl_messages
WHERE
msg_sender_id = '$this->UM_index'
";
$res = $this->db->returnArrayOfObject($sql);
$this->assign_values('sent_messages',$res);
And in the templates I retrieved :
{foreach name = feach key = indx item = k from = $sent_messages}
{$k->msg_sender_id}
{$k->msg_subject}
{/forach}
I can retriev the above two fields. But I want to retrieve the message_sender_name for the field sender_id which can be obtained from a function getDetails($id).
My question is how can I assign the sender_name fields (which is a array) to my smarty template?

So you want also want to be able to have the message_sender_name, wich is returned by the function getDetails($id)?
You will have to loop trough your results, and fetch the message_sender_name for each row. Something like this:
$arrResults = array();
while ($res = $this->db->returnArrayOfObject($sql)))
{
$res['msg_sender_name'] = getDetails($res['msg_sender_id']);
$arrResults[] = $res;
}
$this->assign_values('sent_messages',$arrResults);
If I'm right, you can now access the sender_name via {$k->msg_sender_name} in your template-file.
But I would suggest that you use a JOIN in your SQL-statement, so you don't have to make another request for the sender_name.

Related

How to add Distinct function to a array or return only unique values

How to add distinct function so that the result is not repetitive?
Any help is much appreciated.
$yearnow=$_POST['yearnow'];
$stmts = $db->query("select * from tblgencol where year='$yearnow'");
while($row= $stmts->fetch(PDO::FETCH_OBJ)){
$pdf->Row(Array($row->busname,$row->business));
}
}
Here is the output of my code:
I'm using fpdf to display my data from database. And its working fine. Its just that the name is repeating.
This is what I want to achieve:
or what if i want to display this?
option 2
To achieve your desired result, you need to check if the current business name is the same as the last displayed one, and if so, not output it:
$yearnow=$_POST['yearnow'];
$stmts = $db->query("select * from tblgencol where year='$yearnow'");
$busname = '';
while ($row = $stmts->fetch(PDO::FETCH_OBJ)) {
$thisbusname = ($row->busname != $busname) ? $row->busname : '';
$pdf->Row(array($thisbusname,$row->business));
$busname = $row->busname;
}

Remove Array From Json in PHP

hi i have a backend with php in cpanel and i have a problem with one of jsons . this is part of my php code :
...
}elseif ($work == "dollardate") {
$query3 = "SELECT * FROM tabl_dollar_date";
$result3 = $connect->prepare($query3);
$result3->execute();
$out3 = array();
while ($row3 = $result3->fetch(PDO::FETCH_ASSOC)) {
$record3 = array();
$record3["dollar"] = $row3["dollar"];
$record3["date"] = $row3["date"];
array_push($out3, $record3);
}
echo json_encode($out3);
}
?>
this code show this in json :
[
{
"dollar":"15000",
"date":"1397-12-12"
}
]
how can remove array from json and show the json like this :
{
"dollar":"15000",
"date":"1397-12-12"
}
Easiest way (according his code):
change line
echo json_encode($out3);
to
echo json_encode($out3[0]);
One solution is that if you just want the latest value (in case there are multiple records in the table), then change the SELECT to order by date descending also set LIMIT to 1 to only get the 1 record anyway, and remove the loop to fetch the data and just fetch the 1 record...
$query3 = "SELECT `date`, `dollar`
FROM `tabl_dollar_date`
ORDER BY `date` desc
LIMIT 1";
$result3 = $connect->prepare($query3);
$result3->execute();
$row3 = $result3->fetch(PDO::FETCH_ASSOC);
echo json_encode($row3);
As you know which fields you want from the SELECT, it's good to just fetch those fields rather than always using *. This also means that as the result set only contains the fields your after, you can directly json_encode() the result set rather than extracting the fields from one array to another.

How to create variables using rows from mysql?

I would like to create a file like this:
<?php
$cssMainVersion = "1.0.3";
$cssBootStrapVersion = "1.0.1";
$cssElseVersion = "1.0.4";
$jpgVersion = "1.0.1";
$jsMainVersion = "1.0.1";
$jsElseVersion = "1.0.1";
?>
I have created a mySQL table that holds the variable name, and the variable data. For example, the first row contains "cssMainVersion" and the second row contains "1.0.3".
How would I loop thorugh each row and print that data in a new file according to the format above?
You can create variables dynamically with php
$query = mysqli_query($conn, 'SELECT id, cachename, cacheversion FROM cache');
while($row = mysqli_fetch_assoc($query)) {
$$row['cachename'] = $row['cacheversion'];
}
the $$ will be evaluted from right to left, so, first $row['field'] will be replaced with this value, let say cssMainVersion, then we have $cssMainVersion

search form php/sql multiple inputs

i've a problem with a search form. It works only if I use all the 4 fields, but if a leave a field empty the while loop echoes out all the table's records.
Can someone please help me?
This is my php code for the search function
<?php
if (isset($_POST['cerca'])){
$cerca_tt = $_POST['tt_carrier'];
$cerca_risorsa = $_POST['risorsa_cerca'];
$cerca_team = $_POST['team_cerca'];
$cerca_linea = $_POST['linea_cerca'];
$sql_cerca = "SELECT * FROM normal WHERE
tt LIKE '%".$cerca_tt."%'
OR risorsa LIKE '%".$cerca_risorsa."%'
OR team LIKE '%".$cerca_team."%'
OR linea LIKE '%".$cerca_linea."%'";
if($sql_cerca) {
$trovati = mysql_query($sql_cerca); ?>
if the POST is blank then the variable is blank thus everything will match like '%%'
for example if $cerca_tt is blank. your query would be
"SELECT * FROM normal WHERE tt like '%%'"
that matches everything.
create the query based on the POST response.
$sel = "SELECT * from normal";
//You will need to deal with the WHERE part of the query.
if (!empty($cerca_tt)){
$sel .= " OR tt like '%".$cerca_tt."'";
}
etc....///
If you only provide one search parameter and the rest are empty strings, which you wrap with %, you are effectively searching everything. You need to build up your query. For example (simple):
$sqlParts = [];
if(isset($_POST['tt_carrier'])) {
$sqlParts[] = "tt LIKE '%".$cerca_tt."%'";
}
if(isset($_POST['risorsa_cerca'])) {
$sqlParts[] = "risorsa LIKE '%".$cerca_risorsa."%'";
}
if(isset($_POST['team_cerca'])) {
$sqlParts[] = "team LIKE '%".$cerca_team."%'";
}
if(isset($_POST['team_cerca'])) {
$sqlParts[] = "linea LIKE '%".$linea_cerca."%'";
}
if(!empty($sqlParts)) {
$sql = "SELECT * FROM normal WHERE " . implode(' OR ', $sqlParts);
}

FLOT - Fluid / Dynamic Charts - PHP MYSQL

I am trying to Plot a Graph with FLOT but I cant get my head around how to get make it Dynamic with multiple series.
I am trying to get Race Data , Eg Laps and Time as the Graph x and y, but also trying to get the Race ID as a new series line for each Rider.
I have tried it at a Loop for each RaceID, and I have tried it as a multidimensional array, But I cant get my head around how to get it formatted to how FLOT wants it.
I can get it to work with 1 Rider:
$GetLapData = mysql_Query("SELECT LapData.*, u.RaceID
FROM `LapData`
left join User as u on LapData.TagID = u.TagID
Where LapData.EventID = '$EventID'
and LapData.RaceNameID = '$RaceNameID'
and LapData.TagID = '$RacingNumber'
ORDER BY `LapData`.`LapNumber` ASC");
while ($row = mysql_fetch_assoc($GetLapData))
{
$dataset1[] = array($row['LapNumber'],$row['LapTimeinSeconds']);
}
and then plot the single Data Set
$(function() {
var d1 = <?php echo json_encode($dataset1) ?>;
$.plot("#placeholder", [ d1 ]);
});
Any Ideas on making it all riders for the Race would be really helpful.
This is a tough question to answer without seeing your database scheme, but I'll give it a shot.
First, let's modify your SQL statement. I'm guessing that this part and LapData.TagID = '$RacingNumber' is what subset's it down to a single rider? Also, what's the purpose of the join, do you want the rider's name from there or some other identifier? I've left it but if you aren't using it, it's a waste...
$GetLapData = mysql_Query("SELECT LapData.TagID, LapData.LapNumber, LapData.LapTimeInSeconds
FROM LapData
LEFT JOIN User AS u ON LapData.TagID = u.TagID
WHERE LapData.EventID = '$EventID'
ANDLapData.RaceNameID = '$RaceNameID'
AND LapData.TagID = '$RacingNumber'
ORDER BY LapData.TagID, LapData.LapNumber ASC");
$allSeries = array(); // our "return value"
$currentSeries = null; // some temp variables
$currentRider = null;
while ($row = mysql_fetch_assoc($GetLapData))
{
$loopRider = $row['TagID']; // get rider for this database row
if ($currentRider === null || $loopRider != $currentRider) // if first loop or new rider
{
if ($currentSeries !== null)
{
$allSeries[] = $currentSeries; // we have a new rider push the last one to our return array
}
$currentSeries = array(); // this is first loop or new rider, re-init current series
$currentSeries["label"] = $loopRider;
$currentSeries["data"] = array();
}
$currentSeries["data"][] = array($row['LapNumber'],$row['LapTimeinSeconds']); //push row data into object
}
$allSeries[] = $currentSeries; // last rider's data push
In the end, allSeries is an array of associative arrays per flots documentation.
Mainly:
$allSeries = [{
"label" = "tag1",
"data" = [[0,12],[1,45],[2,454]]
},{
"label" = "tag2",
"data" = [[0,122],[1,415],[2,464]]
}]
After you Json encode this, the call is:
$.plot("#placeholder", allSeries);
My Standard PHP disclaimer, I'm not a PHP programmer, I've never used it and never ever want to use it. All the above code was peiced together from quickly reading the documentation and is untested.

Categories