I am at the internship and I am developing a HTML table that has milestones and milestoneparts(in this project we call it milestonefase). Each milestone has a bunch of milestonefases.
As example: Milestone99 has milestonefase 10, milestonefase14.
Those data come from two diffrent database tables.
My array looks like this:
Array
(
[milestonesfases] => Array
(
[10] => Array
(
[milestonefase_id] => int
[milestonefase_titel] => string
[milestonefase_milestone_id] => int
)
[14] => Array
(
[milestonefase_id] => int
[milestonefase_titel] => string
[milestonefase_milestone_id] => int
)
)
[milestone_id] => int
[milestone_titel] => string
[client] => string
[milestone_verkocht_id] => int
)
My question is: How can I loop trough all milestonefases that is connected with milestone 6 (in this case)
My loop looks like this:
foreach ($stones as $milestone)
{
echo '<tr id="'. $milestone['milestone_id'] . '" class="milestone'. $milestone['milestone_id'] . '">';
echo '<td>'. $milestone['milestone_id'] . '</td>';
echo '<td>' . $milestone['milestone_titel'] . '</td>';
echo '<td>'. $milestone['client'] . '</td>';
echo '</tr>';
for ($i = 0; $i < count($milestone['milestonesfases']); $i++)
{
echo '<tr>';
echo '<td>'. $milestone['milestonesfases']['milestonefase_id'] . '</td>';
echo '<td>'. $milestone['milestonesfases'][10]['milestonefase_titel'] . '</td>';
echo '<td></td>';
echo '</tr>';
}
}
That '10' in my loop needs to be all milestonefase_id's (which is 10, 11, 12, 13, 14, 15 in this milestone).
How can I manage to get all milestonefases.
Attention! it must work for every milestones not only this
please
That piece of milestones works but milestonefases does not work.
This is Check for if is array and you can use it using same foreach like #Aron Said:
foreach ($stones as $milestone)
{
echo '<tr id="'. $milestone['milestone_id'] . '" class="milestone'. $milestone['milestone_id'] . '">';
echo '<td>'. $milestone['milestone_id'] . '</td>';
echo '<td>' . $milestone['milestone_titel'] . '</td>';
echo '<td>'. $milestone['client'] . '</td>';
echo '</tr>';
if(is_array($milestone['milestonesfases'])){
foreach ($milestone['milestonesfases'] as $key => $value) {
echo '<tr>';
echo '<td>'. $value['milestonefase_id'] . '</td>';
echo '<td>'. $value['milestonefase_titel'] . '</td>';
echo '<td></td>';
echo '</tr>';
}
}
}
Instead of using a for loop,
for ($i = 0; $i < count($milestone['milestonesfases']); $i++)
Do a foreach loop
foreach ($milestone['milestonesfases'] as $milestonesfase)
{
echo '<tr>';
echo '<td>'. $milestonesfase['milestonefase_id'] . '</td>';
echo '<td>'. $milestonesfase['milestonefase_titel'] . '</td>';
echo '<td></td>';
echo '</tr>';
}
please try
foreach ($stones as $milestone)
{
echo '<tr id="'. $milestone['milestone_id'] . '" class="milestone'. $milestone['milestone_id'] . '">';
echo '<td>'. $milestone['milestone_id'] . '</td>';
echo '<td>' . $milestone['milestone_titel'] . '</td>';
echo '<td>'. $milestone['client'] . '</td>';
echo '</tr>';
foreach($milestone['milestonesfases'] as $milestoneRow)
{
if($milestoneRow['milestonefase_milestone_id'] == $milestone['milestone_id']) {
echo '<tr>';
echo '<td>'. $milestoneRow['milestonefase_id'] . '</td>';
echo '<td>'. $milestoneRow['milestonefase_titel'] . '</td>';
echo '<td></td>';
echo '</tr>';
}
}
}
Related
i have one table in locallhost ,In my table there is an item id,Now I want to send the id Related row to another page by clicking on EDIT . Thankful
The part of the program I think is difficult to write below
....
....
$response["travel"]=array();
while ($row = mysql_fetch_array($sql)) {
// Print out the contents of the entry
echo '<tr>';
echo '<td class="text-center">' . $i . '</td>';
echo '<td class="text-center">' . $row['companyname'] . '</td>';
echo '<td class="text-center">' . $row['cod'] . '</td>';
echo '<td class="text-center">' . $row['bigan'] . '</td>';
echo '<td class="text-center">' . $row['stop'] . '</td>';
echo '<td class="text-center">' . $row['date'] . '</td>';
echo '<td class="text-center">' . $row['time'] . '</td>';
echo '<td class="text-center">' . $row['price'] . '</td>';
echo '<td class="text-center">' .'EDIT' .'</td>';
$i++;
.......
.....
You don't use <?php inside strings, you use that when you've gone back into HTML mode. You should just concatenate the variable, like you do everywhere else
echo '<td class="text-center">' .'ویرایش' .'</td>';
This code works but the one belows shows an error
if(isset($_POST['month'])=='')
{
$sql = ('SELECT
substring(pin,5,2) as District,
count(arpn) as RPU,
sum(area) as AREA,
sum(marketvalue) as MV,
sum(assessedvalue) as AV
FROM 2017_oct_land
WHERE taxability= "T"
group by District ASC');
foreach ($pdo->query($sql) as $row) {
echo '<tr>';
echo '<td>'. $row['District'] . '</td>';
echo '<td class="RPU">'. $row['RPU'] . '</td>';
echo '<td class="AREA">'. $row['AREA'] . '</td>';
echo '<td class="MV">'. $row['MV'] . '</td>';
echo '<td class="AV">'. $row['AV'] . '</td>';
}
include 'total.php';
}
--with AND-- why do i get this "Warning: Invalid argument supplied for foreach() in C:\wamp64\www\reportview\pages\code2.php on line 20" with this code?
if(isset($_POST['month'])!=NULL)
{
$kind = $_POST['kind'];
echo "MONTH : ".$kind."<br/>";
$sql = ('SELECT
substring(pin,5,2) as District,
count(arpn) as RPU,
sum(area) as AREA,
sum(marketvalue) as MV,
sum(assessedvalue) as AV
FROM julcons
WHERE taxability="T" and actualuse like "' . $kind .'"');
foreach ($pdo->query($sql) as $row) {
echo '<tr>';
echo '<td>'. $row['District'] . '</td>';
echo '<td class="RPU">'. $row['RPU'] . '</td>';
echo '<td class="AREA">'. $row['AREA'] . '</td>';
echo '<td class="MV">'. $row['MV'] . '</td>';
echo '<td class="AV">'. $row['AV'] . '</td>';
}
include 'total.php';
}
Here you go ( untested ) but should be close
if(isset($_POST['kind'])) {
//why check month and then use kind? which is it..
//isset returns boolean, not null, null works because it's false but it's not correct IMO
$kind = $_POST['kind']; //what is this kind or month
echo "MONTH : ".$kind."<br/>";
$sql = 'SELECT
substring(pin,5,2) as District,
count(arpn) as RPU,
sum(area) as AREA,
sum(marketvalue) as MV,
sum(assessedvalue) as AV
FROM julcons
WHERE taxability="T" and actualuse like :kind';
$stmt = $pdo->prepare($sql);
$stmt->execute([':kind' => $kind]);
while( false !== ( $row = $stmt->fetch(PDO::FETCH_ASSOC))){
echo '<tr>';
echo '<td>'. $row['District'] . '</td>';
echo '<td class="RPU">'. $row['RPU'] . '</td>';
echo '<td class="AREA">'. $row['AREA'] . '</td>';
echo '<td class="MV">'. $row['MV'] . '</td>';
echo '<td class="AV">'. $row['AV'] . '</td>';
}
include 'total.php';
}
Sorry, but you had some really big mistakes in your code.
May i ask how to convert an array to JSON format,
I have tried to used the following method, but fail to do so, any idea?
Here's my current result:
["B型肝炎疫苗下一個注射期为2017-06-30 ","96709394"]
["疫苗名稱4下一個注射期为2017-06-30 ","96709394"]
What i want :
{"B型肝炎疫苗下一個注射期为2017-06-30 ","96709394"},
{"疫苗名稱4下一個注射期为2017-06-30 ","96709394"}
Thank you
while($row = mysql_fetch_array( $result )) {
echo "<tr>";
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['patientid'] . '</td>';
echo '<td>' . $row['vaccineid'] . '</td>';
echo '<td>' . $row['vaccinename1'] . '</td>';
echo '<td>' . $row['totalnoofinjection'] . '</td>';
echo '<td>' . $row['nthinjection'] . '</td>';
echo '<td>' . $row['date'] . '</td>';
echo '<td>' . $row['nextdate'] . '</td>';
echo '<td>' . $row['skip'] . '</td>';
echo '<td>' . $row['traditionalmessage'] ."<br />";
echo '<td>' . $row['traditionalmessage'] . '</td>';
echo '<td>' . $row['telMobile'] . '</td>';
$data_array = [$row['traditionalmessage'],$row['telMobile']];
foreach($data_array as $key => $value){
$new_data_array[urlencode($key)] = urlencode($value);
}
$data_json_url = json_encode($new_data_array);
$data_json = urldecode($data_json_url);
echo $data_json;
What´s is wrong on this sentence?
foreach ($pdo->query($sql) as $row) {
echo '<tr>';
echo '<td>'. $row['u_id'] . '</td>';
echo '<td>'. $row['u_role'] . '</td>';
echo '<td>'. $row['u_name'] . '</td>';
echo '<td>'. $row['u_passw'] . '</td>';
echo '<td>'. $row['u_init'] . '</td>';
echo '<td>'. $row['c_name'] . '</td>';
echo '<td>'. $row['u_mail'] . '</td>';
echo '<td>'.'<img href="'. $row['u_pic'] . '" width=45 height=45></img>'.'</td>';`
}
I´m able to print all fields after query but for last one is not possible, I got on db saved the url for the picture with this format
http://localhost/Pics/st.gif dbfield as varchar
If I inspect the component in the browser I can see it was properly selected by mysql but there is not print on the screen.
Any comment? Thanks in advance
For images you'll have to use:
<img src="url">
echo '<td><img src="'. $row['u_pic'] . '" width=45 height=45></img></td>';
It should be in following way,
foreach ($pdo->query($sql) as $row) {
echo '<tr>';
echo '<td>'. $row['u_id'] . '</td>';
echo '<td>'. $row['u_role'] . '</td>';
echo '<td>'. $row['u_name'] . '</td>';
echo '<td>'. $row['u_passw'] . '</td>';
echo '<td>'. $row['u_init'] . '</td>';
echo '<td>'. $row['c_name'] . '</td>';
echo '<td>'. $row['u_mail'] . '</td>';
echo '<td>'.'<img src="'. $row['u_pic'] . '" width=45 height=45></img>'.'</td>';
}
I have a foreach loop that iterates over objects in an array. Currently, it outputs information for every element. I need to print out data for only the objects that have an "a" at the end of their id property. Code is as follows:
foreach ($obj as $key => $value) {
echo '<tr id="subRow">';
echo '<td>' . $value->date . '</td>';
echo '<td class="leftLine">' . $value->Name . '</td>';
echo '<td class="leftLine">' . $value->dealType . '</td>';
echo '<td class="leftLine">' . $value->id . '</td>';
echo '<td class="leftLine">' . 'Adobe PDF' . '</td>';
echo '</tr>';
}
So for every element of the $obj that contains an ID number with an "a" at the end of it, print out that entire element.
substr can help with that:
foreach ($obj as $key => $value) {
if(substr($value->id, -1) === 'a') {
echo '<tr id="subRow">';
echo '<td>' . $value->date . '</td>';
echo '<td class="leftLine">' . $value->Name . '</td>';
echo '<td class="leftLine">' . $value->dealType . '</td>';
echo '<td class="leftLine">' . $value->id . '</td>';
echo '<td class="leftLine">' . 'Adobe PDF' . '</td>';
echo '</tr>';
}
}