I am retrieving JSON data in array and I sort them into the HTML table. So far it is OK, but I would like to sum the specific values from JSON Response and print the sum of object values.
I have a PHP code which sorting me the data from JSON as following:
foreach($result->response as $value)
{
echo "<tr>";
echo "<td>" . $value->datetime . "</td>";
echo "<td>" . $value->service_type . "</td>";
echo "<td>" . $value->destination . "</td>";
echo "<td>" . $value->duration . "</td>";
echo "<td>" . $value->price . "</td>";
echo "</tr>";
}
I am interested only in values of prices (in JSON Array), I would like to take only the value of prices from JSON and sum the values of prices and give the result number under the table.
I am trying this without any success:
$countprice = $result->response->price;
$totalprice = count($countprice);
echo $totalprice;
The result is weird number, I know that PHP use "count", but I am not sure how to use it. Sorry I am novice in JSON and PHP and I will thank you for any tip you may give me.
Try this
<?php
$sum = 0;
foreach ($result->response as $value) {
echo "<tr>";
echo "<td>" . $value->datetime . "</td>";
echo "<td>" . $value->service_type . "</td>";
echo "<td>" . $value->destination . "</td>";
echo "<td>" . $value->duration . "</td>";
echo "<td>" . $value->price . "</td>";
echo "</tr>";
$sum += $value->price;
}
echo "sum is {$sum}";
Related
I have created a database in phpMyAdmin and I have 10 rows of data and each row has a column to show its website address. I want to make the website address clickable.
I used the below array method to show my website address link, but it doesn't work
$website = array(
array("Google","https://code.tutsplus.com"),
array("Bing","https://weatherstack.com"),
array("W3","https://www.w3schools.com")
);
foreach ($website as $urlitem){
echo "<a href='".$urlitem[1]."'></a>";
}
// this gets an associative array (ie the keys can be used as well as the indicies)
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
// The below code displays my table
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['type'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['street'] . "</td>";
echo "<td>" . $row['city'] . "</td>";
echo "<td>" . $row['region'] . "</td>";
echo "<td>" . $row['code'] . "</td>";
echo "<td>" . $row['phone'] . "</td>";
echo "<td><a href='".$urlitem[0]."'>" .$row["website"] ."</td>";
echo "</tr>";
}
}
In the foreach loop, the issue is you are not print out anything to be displayed for anchor tag. Means you anchor tag exists but it does not have any content to be shown on html;
YOUR CONTENT MISSING PART
After that in your while loop, you are using the variable $urlitem which does not exists. You either need to declared it outside your foreach or need to use the $website variable to get the url.
I am using the below php code to list Alphabetical(A-Z) characters in the rows. It works fine from a to z. My problem is that after "z", it starts with aa, ab,ac, etc.. I'd like it to start with a to z again every time it comes to z.
How can achieve this? thanks.
<?php
foreach (range('a', 'z') as $char)
while($row = mysqli_fetch_array($search_result))
{
echo "<tr>";
echo "<td>" .$char. "</td>";
$char++;
echo "<td>" . $row['Rp1'] . "</td>";
echo "<td>" . $row['Rp2'] . "</td>";
echo "<td>" . $row['Rp3'] . "</td>";
echo "<td>" . $row['Rp4'] . "</td>";
echo "</tr>";
}
?>
I tried below solution but it ended up with the same result.
<?php
$char= 'a';
for($i=0;$i<26;$i++)
while($row = mysqli_fetch_array($search_result))
{
echo "<tr>";
echo "<td>" .$char. "</td>";
$char++;
echo "<td>" . $row['Rp1'] . "</td>";
echo "<td>" . $row['Rp2'] . "</td>";
echo "<td>" . $row['Rp3'] . "</td>";
echo "<td>" . $row['Rp4'] . "</td>";
echo "</tr>";
}
?>
You could utilize PHP's built-in array functions for handling internal array pointers:
$range = range('a', 'z');
while ($row = mysqli_fetch_array($search_result)) {
echo "<tr>";
echo "<td>" . current($range) . "</td>";
echo "<td>" . $row['Rp1'] . "</td>";
echo "<td>" . $row['Rp2'] . "</td>";
echo "<td>" . $row['Rp3'] . "</td>";
echo "<td>" . $row['Rp4'] . "</td>";
echo "</tr>";
if (next($range) !== false) {
reset($range);
}
}
Checkout these references to learn more:
http://php.net/manual/en/function.current.php
http://php.net/manual/en/function.next.php
http://php.net/manual/en/function.reset.php
You can reference your array of letters by a calculated value, $i % 26, where $i is the zero based iteration count of your while loop.
$letters = range('a', 'z');
$i = 0;
while ($row = mysqli_fetch_array($search_result)) {
echo "<tr>";
echo "<td>" . $letters[$i++ % 26] . "</td>";
echo "<td>" . $row['Rp1'] . "</td>";
echo "<td>" . $row['Rp2'] . "</td>";
echo "<td>" . $row['Rp3'] . "</td>";
echo "<td>" . $row['Rp4'] . "</td>";
echo "</tr>";
}
Firstly, get the characters from a to z in an array.
Initialize a counter (index) to access a specific character from the array.
Increment the counter inside loop.
If the counter value goes above 25 (index starts from 0 - so this represents 26th array item), reset its value back to zero.
Try the following:
<?php
// get array for characters from a to z
$characters = range('a', 'z');
$counter = 0; // initialize the counter (index)
while($row = mysqli_fetch_array($search_result))
{
echo "<tr>";
// access the character using counter index
echo "<td>" . $characters[$counter] . "</td>";
// Increment the counter
$counter++;
// Check and reset if the counter has gone above 25 (26th array item)
$counter = ($counter > 25 ? 0 : $counter);
echo "<td>" . $row['Rp1'] . "</td>";
echo "<td>" . $row['Rp2'] . "</td>";
echo "<td>" . $row['Rp3'] . "</td>";
echo "<td>" . $row['Rp4'] . "</td>";
echo "</tr>";
}
?>
I am using this code to get that data from SQL Server database using PHP
<?php
foreach ($dbDB->query($query) as $row) {
echo "<tr>";
echo "<td>" . $row['Country'] . "</td>";
echo "<td>" . $row['OrderNumber'] . "</td>";
echo "<td>" . $row['Region'] . "</td>";
echo "<td>" . $row['ShipDate'] . "</td>";
echo "<td>" . $row['ProducedDate'] . "</td>";
echo "</tr>"; }
?>
I am trying to replace these multiple lines but storing the columns' names in a string for example $_POST['SelectedColumns'].
The values coming into post as comma separated string, For example : Country,OrderNumber,Region,ShipDate,ProducedDate
I have tried this solution but still not working for me.
<?php
$ser="********";
$db="******";
$user="******";
$pass="******";
$query = 'SELECT '.$_POST['SelectedColumns'].' FROM reporting.REPORT_ALL';
$dbDB = new PDO("odbc:Driver=ODBC Driver 13 for SQL Server;Server=*******;Database=******;Port=1456", $user, $pass);
$row = $_POST["SelectedColumns"];
$rows = explode(",",$row);
/*Here I have the another html code independent of this part */
foreach ($dbDB->query($query) as $dataRow) {
echo "<table>";
echo "<tr>";
foreach ($rows as $r ) {
echo "<td>" . $dataRow[$r] . "</td>"; }
echo "</tr>";
echo "</table>"; }
?>
Any suggestions please ?
I am attempting to fetch data from my database using 8 different tables named:
inventory descriptorid typeid conditionid statusid locationid stationid users
I believe my query is working correctly the problem is using my $row variable. I am only getting the results from data in the inventory table. Can Someone explain how I can fetch my data from each table?
The code is below:
<?php
// get the records from the database
if ($result = $con->query("
SELECT inventory.InventoryID, descriptorid.dename, typeid.tyname,
inventory.Serial, inventory.ServiceTag, inventory.CityTag,
conditioncid.conname, statusid.statusname, locationid.locname,
stationid.stationname, users.Fname, users.Lname,
inventory.PurchaseDate, inventory.POnumber
FROM inventory
LEFT JOIN descriptorid
ON inventory.Descriptor = descriptorid.dename
LEFT JOIN typeid
ON inventory.Type = typeid.tyname
LEFT JOIN conditioncid
ON inventory.ConditionC = conditioncid.conname
LEFT JOIN statusid
ON inventory.Status = statusid.statusname
LEFT JOIN locationid
ON inventory.Location = locationid.locname
LEFT JOIN stationid
ON inventory.Station = stationid.stationname
LEFT JOIN users
ON inventory.cuserFname = users.Fname
AND inventory.cuserLname = users.Lname "))
{
// display records if there are records to display
if ($result->num_rows > 0)
{
// display records in a table
echo "<table border id='myTable' class='tablesorter' >";
// set table headers
echo "<thead><th>ID</th><th>Descriptor</th><th>Type</th><th>Serial</th><th>ServiceTag</th><th>CityTag</th><th>Condition</th><th>Status</th><th>Location</th><th>Station</th><th>CurrentUser</th><th>Lastname</th><th>PurchaseDate</th><th>POnumber</th><th></th><th></th></thead>";
echo "<tbody";
while ($row = $result->fetch_object())
{
// dump whats returned
// print_r($row);
echo "<tr>";
echo "<td>" . $row->InventoryID . "</td>";
echo "<td>" . $row->Descriptor . "</td>";
echo "<td>" . $row->Type . "</td>";
echo "<td>" . $row->Serial . "</td>";
echo "<td>" . $row->ServiceTag . "</td>";
echo "<td>" . $row->CityTag . "</td>";
echo "<td>" . $row->ConditionC . "</td>";
echo "<td>" . $row->Status . "</td>";
echo "<td>" . $row->Location . "</td>";
echo "<td>" . $row->Station . "</td>";
echo "<td>" . $row->cUserFname . "</td>";
echo "<td>" . $row->cUserLname . "</td>";
echo "<td>" . $row->PurchaseDate . "</td>";
echo "<td>" . $row->POnumber . "</td>";
echo "<td><a href='invrecords.php?InventoryID=" . $row->InventoryID . "'><img src='img/default/button_mini_ticket_edit.gif'></a></td>";
echo '<td><img src="img/default/button_mini_delete.gif">';
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
You should name your row vars as you select them (as you did it with the columns of the inventory table):
Wrong:
echo "<td>" . $row->Descriptor . "</td>";
echo "<td>" . $row->Type . "</td>";
echo "<td>" . $row->ConditionC . "</td>";
echo "<td>" . $row->Status . "</td>";
echo "<td>" . $row->Location . "</td>";
echo "<td>" . $row->Station . "</td>";
echo "<td>" . $row->cUserFname . "</td>";
echo "<td>" . $row->cUserLname . "</td>";
Use instead:
echo "<td>" . $row->dename . "</td>";
echo "<td>" . $row->tyname. "</td>";
echo "<td>" . $row->conname. "</td>";
echo "<td>" . $row->statusname. "</td>";
echo "<td>" . $row->locname. "</td>";
echo "<td>" . $row->stationname. "</td>";
echo "<td>" . $row->Fname. "</td>";
echo "<td>" . $row->Lname. "</td>";
Are you sure that you want use LEFT JOIN instead of INNER JOIN? That means that if you have table A and B you only take elements which are located in A but not necessarily in B. That means if there is not a matching partner for an element of the left table, you would have a row containg an element of table A in one column and null in the other column.
This might be one reason for having a resultset only containing an element of the inventory table.
As far as I understand you want a result containing the elements joined on equal attributes, so try to replace LEFT JOIN with INNER JOIN.
Image source: http://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins
Ok so the issue was instead of matching the third option (dename, tyname,etc.) on my secondary tables with the inventory I should've been matching it with the ID of that said table. Now when I query the third option with my row variable I can select any option from my secondary table (deid,tyid). It now outputs the desired results and hopefully I can create a drop down list using a similar query.Thanks for all the ideas.
echo '<td><input type="checkbox" name="items[]" value="' . $row['0'] . '" /></td>';
Hi, I'm trying to get a reference number which comes from a an array called items from another page as shown above, and to find it in the table and print out the reference row, like "Title,Platform...." into another table, but I can't seem to get it working...any help be appreciated
if (isset($_POST['items'])) {
$n = count($_POST['items']);
for($i=0; $i < $n; $i++){
// echo $_POST['items'][$i];
}
$items = array();
foreach ($_POST['items'] as $item) {
$items[] = pg_escape_string($con, $item);
}
if (!$_SESSION["selectingrows"]) {
$item_string = "'" . implode("','", $items) . "'";
$result = pg_query ($con, "SELECT title, platform, description, price FROM CSGames WHERE 'refnumber' IN ($item_string)");
while($rows = pg_fetch_assoc($result))
{
echo "<tr>";
echo "<td>" . $rows['1'] . "</td>";
echo "<td>" . $rows['2'] . "</td>";
echo "<td>" . $rows['3'] . "</td>";
echo "<td>" . $rows['4'] . "</td>";
echo "</tr>";
}
}
}
One thing, you need to put {} braces after your while loop.
Here is what you are doing:
while($rows = pg_fetch_assoc($result))
echo"<tr>"; echo "<td>" . $rows['1'] . "</td>"; echo "<td>" . $rows['2'] . "</td>"; echo "<td>" . $rows['3'] . "</td>"; echo "<td>" . $rows['4'] . "</td>";
echo"</tr>";
By not putting braces around the code after the while statement, here is what your code really does:
while($rows = pg_fetch_assoc($result))
{
echo"<tr>";
}
echo "<td>" . $rows['1'] . "</td>"; echo "<td>" . $rows['2'] . "</td>"; echo "<td>" . $rows['3'] . "</td>"; echo "<td>" . $rows['4'] . "</td>";
echo"</tr>";
You should always put braces in to define what code is in the while loop.
You want your code to be something like this:
while($rows = pg_fetch_assoc($result))
{
echo "<tr>";
echo "<td>" . $rows['1'] . "</td>";
echo "<td>" . $rows['2'] . "</td>";
echo "<td>" . $rows['3'] . "</td>";
echo "<td>" . $rows['4'] . "</td>";
echo "</tr>";
}
Format your code neatly and properly. By doing this your code is clearer and it is much easier to notice possible mistakes like the above. Always use braces for if, while, for statements. When putting an end line semicolon ; put in a new line break. Indent your code correctly. It's little things like formatting that make coding easier.
Now the next problem I can see is the values you are getting from the $rows array:
$rows['1'];
$rows['2'];
$rows['3'];
$rows['4'];
This is trying to get something from the $rows array which has the key of string '1'.
Usually you access array values by index, which uses an integer beggining from 0. Or you access it by a key.
Either you can try this:
$rows[0];
$rows[1];
$rows[2];
$rows[3];
Or this:
$rows['title'];
$rows['platform'];
$rows['description'];
$rows['price'];