Hello all,
I have this page that I made: and on the middle column called "Total Flights" and there are many 0's.
I want to be able to change all the 0's to NONE.
How would I do this? Can someone help?
Thanks. :)
Hello all,
I have this page that I made: and on the middle column called "Total Flights" and there are many 0's.
I want to be able to change all the 0's to NONE.
How would I do this? Can someone help?
Thanks. :)
The root issue is that the page is building the table by printing each column in separate loops (or by echoing the raw tflts value after the replacement), so the "replace 0 with NONE" never lands on the per-row cell you want. Combine the two ideas already suggested by and : iterate once per pilot (row), read each field, and for the flights field output "NONE" when the value is empty or zero.
A minimal, safe pattern (using SimpleXML) is below — cast node values to strings, trim them, test numerically for zero, and escape output for HTML:
$xml = simplexml_load_file('path/to/roster_xml.php?id=18686') or die('XML error');
echo '<table>';
foreach ($xml as $pilot) {
$first = (string) $pilot->fn;
$last = (string) $pilot->ln;
$tflts = trim((string) $pilot->tflts);
$flightsDisplay = ($tflts === '' || (int) $tflts === 0) ? 'NONE' : $tflts;
echo '<tr>'
. '<td>' . htmlspecialchars($first, ENT_QUOTES, 'UTF-8') . '</td>'
. '<td>' . htmlspecialchars($last, ENT_QUOTES, 'UTF-8') . '</td>'
. '<td>' . htmlspecialchars($flightsDisplay, ENT_QUOTES, 'UTF-8') . '</td>'
. '</tr>';
}
echo '</table>'; The warnings you saw after assigning like $pilot_first_name->fn = $row[0]; come from using the object/array operators incorrectly. Read node text into scalars (as above) or reference the SimpleXMLElement children directly — do not try to create properties on undefined scalars. To debug structure, dump one element with var_dump($xml) or var_dump($pilot) so you can confirm the child names.
Additional notes: prefer (int) or intval() to detect numeric zero so you do not accidentally change other numbers, and always run the replacement at the moment you build the row (not in a separate column-wise loop). See the PHP SimpleXML docs for details: SimpleXML documentation and use htmlspecialchars when injecting XML text into HTML.
Jump to Post— R0bb0b 344we can start here:
$strMiddleColumnValue = "0"; echo trim($strMiddleColumnValue) == "0"?"NONE":$strMiddleColumnValue;
Jump to Post— Member #120589<td><center><?php if($xml === false){ echo "There was an error opening the xml file."; exit; }else{ foreach($xml as $pilot_totalflights_flown) { echo $pilot_totalflights_flown->tflts . "<br>"; } } ?></center></td>This looks like you've got a single cell with all the data separated by a <br />.
Where does all the …
we can start here:
$strMiddleColumnValue = "0";
echo trim($strMiddleColumnValue) == "0"?"NONE":$strMiddleColumnValue; Okay this works, but not replacing the "0". I have uploaded the file to my server and this is what I got.
Here is the code for my loop too get data from the XML, and this is a table.
<td><center><?php if($xml === false)
{
echo "There was an error opening the xml file.";
exit;
}
else
{
foreach($xml as $pilot_totalflights_flown)
{
echo $pilot_totalflights_flown->tflts . "<br>";
}
}
?></center></td> Here is where i put your code you gave me.
<td><center><?php if($xml === false)
{
echo "There was an error opening the xml file.";
exit;
}
else
{
foreach($xml as $pilot_totalflights_flown)
{
$strMiddleColumnValue = "0";
echo trim($strMiddleColumnValue) == "0"?"NONE":$strMiddleColumnValue;
echo $pilot_totalflights_flown->tflts . "<br>";
}
}
?>
<td><center><?php if($xml === false){
echo "There was an error opening the xml file.";
exit;
}else{
foreach($xml as $pilot_totalflights_flown) {
echo $pilot_totalflights_flown->tflts . "<br>";
}
}
?></center></td> This looks like you've got a single cell with all the data separated by a <br />.
Where does all the other data come from?
Don't tell me you have similar code for the rest of the columns.
You need to do a loop ACROSS rather than DOWN. This means you can target the fourth value [position 3 within the row (0,1,2,3)] and just run the replacement on that.
I assume the following xml as an array of items (rows) in the $data array:
foreach($data as $row){
$first = $row[0];
$last = $row[1];
$status = $row[2];
$flights = (intval($row[3]) == 0) ? 'NONE' : $row[3];
$rating = $row[4];
$country = $row[5];
echo "<tr><td>$first</td><td>$last</td><td>$status</td><td>$flights</td><td>$rating</td><td>$country</td></tr>";
} Here is where I get my XML data from: 18686
And yes I do have similar code for the other columns, such as country: I just change:
foreach($xml as $pilot_totalflights_flown) {
echo $pilot_totalflights_flown->tflts . "<br>"; to:
foreach($xml as $pilot_country) {
echo $pilot_country->cn . "<br>"; Here is a page I made to show you the tags such as | cn | tflts | etc...
When I try this:
<?php foreach($xml as $row){
$pilot_first_name->fn = $row[0];
$pilot_last_name->ln = $row[1];
$pilot_status->sta = $row[2];
$pilot_totalflights_flown->tflt = (intval($row[3]) == 0) ? 'NONE' : $row[3];
$pilot_rating->rating = $row[4];
$pilot_country->cn = $row[5];
echo "<table><tr><td>$pilot_first_name</td><td>$pilot_last_name</td><td>$pilot_status</td><td>$pilot_totalflights_flown</td><td>$pilot_rating</td><td>$pilot_country</td></tr></table>";
}
?> Obviously I did edit the original.
It gives me a crap load of errors all the same as this but throughout the whole page and it is the exact same:
Warning: main() [function.main]: Cannot add element pilot number 1 when only 0 such elements exist in C:\xampp\htdocs\web app.php on line 173
Warning: main() [function.main]: Cannot add element pilot number 2 when only 0 such elements exist in C:\xampp\htdocs\web app.php on line 174
Warning: main() [function.main]: Cannot add element pilot number 3 when only 0 such elements exist in C:\xampp\htdocs\web app.php on line 175
Warning: main() [function.main]: Cannot add element pilot number 4 when only 0 such elements exist in C:\xampp\htdocs\web app.php on line 176
Warning: main() [function.main]: Cannot add element pilot number 5 when only 0 such elements exist in C:\xampp\htdocs\web app.php on line 177
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.