trying to join to 2 xml files with the same value , but when i echo the result it separates the value .
foreach(glob("tarea/*.{assemble.xml,report.xml}", GLOB_BRACE) as $filename) {
$Keys =simplexml_load_file($filename);
foreach($Keys as $Key) {
$key1 = $Keys->ProductKeyID;
$pk = $Keys->ProductKey;
$key2 = $Keys->ProductKeyID;
$serial = $Keys->SerialNumber;
if ($key1 == $key2) {
echo "<tr>";
echo "<td>" .$pk."</td>";
echo "<td>" .$key1."</td>";
echo "<td>" .$key2."</td>";
echo "<td>" . $serial."</td>";
echo "</tr>";
break;
}
}
}
is there any way of putting something like
if key1 = key2 join the together
this is my result im hoping to put all in one line .
ProductKey ProductKeyID ProductKeyID SerialNumber
1234 3305492290985 3305492290985
3305492290985 3305492290985 23
Related
This question already has answers here:
variable variables bad practice to use?
(2 answers)
Closed 9 months ago.
The following SQL PHP code shows count of country from database and displays it in a table.
Here is the code
foreach($dbh->query('SELECT country,COUNT(*)
FROM data_able
GROUP BY country') as $row) { $array = array();
echo "<tr>";
echo "<td>" . $row['country'] . "</td>";
echo "<td>" . $row['COUNT(*)'] . "</td>";
echo "</tr>";
}
Here is the result
Here is the result
I would like to use the data as $country1 and $count1.
For example $country1 will output "Andorra" and $country5 will be "India".
and $count5 should give "25" as result. How can I do that ?
Here is an example how to declare variables dynamicly:
$array = ['1', '2', '3', '4', '5'];
foreach ($array as $i) {
${'country' . $i} = $i;
}
echo $country2;
You just need to iterate through your query results with keeping some index on variable suffix (in your case a number).
// create a dynamic variables
$result = $dbh->query('SELECT country,COUNT(*) FROM data_able GROUP BY country');
$i = 1;
foreach ($result as $row) {
${'country' . $i} = $row['country'];
${'count' . $i} = $row['COUNT(*)'];
$i++;
}
// example how to use them
$i = 1;
foreach($result as $row) {
$array = array();
echo "<tr>";
echo "<td>" . ${'country' . $i} . "</td>";
echo "<td>" .${'count' . $i} . "</td>";
echo "</tr>";
$i++;
}
I'm a PHP beginner. I would like to print rows based on specific condition. For Example, I have a set of Data from SQL-Query(shown below) and I want to print all rows of [Series no] only when Type = 210 and Category = J. I know 'foreach' loop should be used in this case, but I do not know how.
And this is what I've tried. I know it is wrong but as I said I am a beginner and self learning.
$get_data = sqlsrv_query($connde, $mainquery);
$data_array = [];
while ($row = sqlsrv_fetch_array($get_data, SQLSRV_FETCH_ASSOC)) {
$data_array[$row["Type"]][$row["Category"]][$row["Series No"]][$row["End Date"]];
}
foreach ($data_array as $value1) {
if ($value1 = 210) {
foreach ($value1 as $value2) {
if isset($value2 == 'J')
echo "<td>" . $row["Series No"] . "</td>";
}
}
}
You can deal with the rows directly:
<?php
$mainquery = 'SELECT `Type`, `Category`, `Series No`, `End Date` FROM `Foo`';
$get_data = sqlsrv_query($connde, $mainquery);
while ($row = sqlsrv_fetch_array($get_data, SQLSRV_FETCH_ASSOC)) {
if($row['Type'] == 210 && $row['Category'] == 'J') {
echo "<td>" . $row['Series No'] . "</td>";
}
}
I'm basically trying to perform a calculation with the first row and first column however I can't seem to get it working correctly.
function tableCreator($height_arrayy, $weight_arrayy) {
echo "<table><tr><th></th>";
foreach($height_arrayy as $value1) {
echo "<th>$value1</th>";
}
echo "</tr>";
foreach($weight_arrayy as $value2) {
echo "<tr>";
echo "<td>$value2</td>";
foreach($weight_arrayy as $value3) {
foreach($height_arrayy as $value4) {
echo "<td>" . ($value3) / ( pow( ($value4 / 100), 2 ) ) . "</td>";
}
}
echo "</tr>";
}
echo "</table>";
}
Result I am getting:
Desired Result:
you have added an un-necessary foreach loop, update your second foreach loop as below
foreach($weight_arrayy as $value2) {
echo "<tr>";
echo "<th>$value2</th>";
foreach($height_arrayy as $value4) {
echo "<td>" . ($value2) / ( pow( ($value4 / 100), 2 ) ) . "</td>";
}
echo "</tr>";
}
your code will return desired output.
use round() if you want to round upto 2 or 3 decimal points.
I'm struggling to create a table made of the joomla database. Currently I'm using the following code:
$query= "SELECT DATUM, A , B ,C, D, E, F, G FROM ".$db->quoteName('#__XYZTABLE'). " WHERE DATUM_ENG>= CURDATE()";
$db->setQuery($query);
$results = $db -> loadObjectList();
echo "<table><tr><th>Name</th><th>Department</th></tr>";
foreach($results as $row){
echo "<tr>"; echo "<td>".$row->last_name."</td>";
echo "<td>".$row->dept."</td>";
echo "</tr>"; }
echo "</table>";
Basically, this code works fine. The result is:
DATUM , A , B ,C, D, E, F, G
xxxx1 ,aa1,bb1,cc1,dd1,ee1,ff1,gg1
xxxx2 ,aa2,bb2,cc2,dd2,ee2,ff2,gg2
....
However, I want to create an html table which transposes the data and shows it like the following form:
xxxx1 , xxxx2, xxxx3 ....
A aa1 , aa2 , aa3
B bb1 , bb2 ,bb3
C cc1 , cc2 ,cc3
D .....
After searching in this forum and Google, I wasn't able to find anything similar. Can someone help me with a specific code?
Using some php array functions, you may take this:
$object = new stdClass();
// create some test values
$object->DATUM = '2016120';
$object->A = 'aa';
$object->B = 'bb';
$object->C = 'cc';
$object->D = 'dd';
$object->E = 'ee';
$object->F = 'ff';
$object->G = 'gg';
for ($i = 1; $i < 10; $i++){
$vars = get_object_vars($object);
$obj = new stdClass;
foreach ($vars as $key=>$var){
$obj->$key = $var.$i;
}
$results[] = $obj;
}
// end of creating test values
echo "<table><tr><th>DATUM</th><th>A</th><th>B</th><th>C</th><th>D</th></tr>";
foreach ( $results as $row ) {
echo "<tr>";
echo "<td>" . $row->DATUM . "</td>";
echo "<td>" . $row->A . "</td>";
echo "<td>" . $row->B . "</td>";
echo "<td>" . $row->C . "</td>";
echo "<td>" . $row->D . "</td>";
echo "<td>" . $row->E . "</td>";
echo "<td>" . $row->F . "</td>";
echo "<td>" . $row->G . "</td>";
echo "</tr>";
}
echo "</table>";
echo '<h3>New Variant</h3>';
echo '<table>';
$elements = count($results);
foreach (get_object_vars($results[0]) as $key=>$var){
echo '<tr>';
$tag = ($key === 'DATUM') ? 'th>' : 'td>';
echo '<'.$tag . $key .'</'.$tag;
for ($i = 0; $i < $elements; $i++){
echo '<'.$tag . $results[$i]->$key.'</'.$tag;
}
echo '</tr>';
}
echo '</table>';`
I have a foreach function which permits me to print fields in a database,
I want to do possible that if $row[value]="photo" don't just print the $row[value] but another thing.
Can I do it?
The code is this:
while($row = mysql_fetch_array($result)) {
echo "<tr>";
foreach($checked1 as $key => $value){
echo "<td>" . $row[$value] . "</td>";
}
I need to have a condition that if $row[value]=photo to echo another thing not: echo "<td>" . $row[$photo] . "</td>";
Any example?
Since i could not get what to print for if($row['value'] == photo) check, so add that code yourself in the code written below.
while($row = mysql_fetch_array($result)){
echo "<tr>";
foreach($checked1 as $key => $value){
if($value == 'photo'){
echo "<td><img src = '".$row[$value]."'></td>";
}else{
echo "<td>" . $row[$value] . "</td>";
}
}
}
This doesn't need to be complex.
foreach ($result as $row) {
if ($row['value'] === 'photo') {
// print "another thing"
} else {
// print "something"
}
}
Sure, just include an if statement with that. Your code will be similar to the code below:
$arr = array("one", "two", "three");
foreach ($arr as $value) {
if($value == "one")
echo "Not gonna print one.";
else
echo "Value: $value<br />\n";
}
Using your code, you can see if the value is not photo, echo it.
while($row = mysql_fetch_array($result)) {
echo "<tr>";
foreach($checked1 as $key => $value){
if($row[value] == "photo") {
echo "some other thing";
} else {
echo "<td>" . $row[$value] . "</td>";
}
}
}
If you want to print img src if the field is "photo" field (key) then
Try this :
while($row = mysql_fetch_array($result)){
echo "<tr>";
foreach($checked1 as $key => $value){
if($key == 'photo'){
echo "<td><img src = '".$row[$value]."'></td>";
}else{
echo "<td>" . $row[$value] . "</td>";
}
}
}