Display Output in Parts in PHP

Display output in parts in PHP

Disable all output buffering and pad the output:

while(@ob_get_clean());

for($i = 0; $i<30; $i++)
{
echo str_pad("$i<br>",4096);
usleep(100000);
}

Also, this won't work, if your Apache is using mod_deflate and you have gzip-compression for text/html files.

Show only a specific part of html response in php

One way to do what you're looking for is use DomDocument to filter out the json data in the source ($file) and then use a recursive function to get the elements you need.

You can set the elements you need using an array, $filter. In this example we've taken a sample of some of the available data, i.e. :

$filter = [
'orderId', 'shortStatus', 'promiseMessage',
'lastTransitionPercentComplete', 'lastReachedMilestone', 'shipmentId',
];

The code

<?php

$filename = 'https://www.amazon.co.uk/progress-tracker/package/ref=pe_3187911_189395841_TE_typ?_encoding=UTF8&from=gp&itemId=&orderId=203-2171364-3066749&packageIndex=0&shipmentId=23796758607302';
$file = file_get_contents($filename);

$trackingData = []; // store for order tracking data
$html = new DOMDocument();
@$html->loadHTML($file);
foreach ($html->getElementsByTagName('script') as $a) {
$data = $a->textContent;
if (stripos($data, 'shortStatus') !== false) {
$trackingData = json_decode($data, true);
break;
}
}

// set the items we need
$filter = [
'orderId', 'shortStatus', 'promiseMessage',
'lastTransitionPercentComplete', 'lastReachedMilestone', 'shipmentId',
];
// invoke recursive function to pick up the data items specified in $filter
$result = getTrackingData($filter, $trackingData);

echo '<pre>';
print_r($result);
echo '</pre>';

function getTrackingData(array $filter, array $data, array &$result = []) {
foreach($data as $key => $value) {
if(is_array($value)) {
getTrackingData($filter, $value, $result);
} else {
foreach($filter as $item) {
if($item === $key) {
$result[$key] = $value;
}
}
}
}
return $result;
}

Output:

Array
(
[orderId] => 203-2171364-3066749
[shortStatus] => IN_TRANSIT
[promiseMessage] => Arriving tomorrow by 9 PM
[lastTransitionPercentComplete] => 92
[lastReachedMilestone] => SHIPPED
[shipmentId] => 23796758607302
)

Showing data from the db on other parts of the page

First of all, a much better approach to this would be to separate your database and view code, for example by using an MVC framework (such as CakePHP).

But here is what I would do if was doing this a similar way to you: (You are only selecting one result from the database, if you were selecting many, this would be different.)
At top of page:

<?php
$email = $_SESSION['email'];
$result = mysqli_query($mysqli,"SELECT * FROM members WHERE email='".$email."'");
while($row = mysqli_fetch_array($result)){
$member = $row;
}
mysqli_close($mysqli);
?>

then whenever you need to display the data in the page: (for example)

My username is <?php echo $member['username']; ?>

How to print part of an Array PHP

You can simply access the value directly:

echo $myArray[0]['product_id'];

You may want to start reading the documentation about arrays in php:
http://php.net/manual/en/language.types.array.php

Using PHP, how do I echo a different output based on the total number in a form?

Do not use isset for your equations (it returns true if the value is set and true is always >0 and <250). So instead of

if (isset($_SESSION['TOTAL']) > 0 && isset($_SESSION['TOTAL']) <= 250) { echo ' $1,000';}

say

if ($_SESSION['TOTAL'] > 0 && $_SESSION['TOTAL'] <= 250) { echo ' $1,000';}

Also use elseif to ensure, only one block is executed:

if ($_SESSION['TOTAL'] > 0 && $_SESSION['TOTAL'] <= 250) {
echo ' $1,000';
} elseif ($_SESSION['TOTAL'] > 0 && $_SESSION['TOTAL'] <= 500) {
echo ' $2,000';
} elseif ($_SESSION['TOTAL'] > 0 && $_SESSION['TOTAL'] <= 1000) {
echo ' $4,000';
} else {
echo 'Sorry, we do not have a price for that.';
}

displaying php in an echo nested in html

Based on Johannes' answer I managed to figure out an answer. I need to close the PHP tags and instead of echoing the wished result, put it in the while loop.

Like this:

    function update_category(){

global $connection;

if (isset($_GET['edit'])) {
$edit_cat_key = $_GET['edit'];

$query = "SELECT * FROM categories WHERE cat_id = {$edit_cat_key }";
$edit_cat_query = mysqli_query($connection, $query);

while($row = mysqli_fetch_assoc($edit_cat_query)) {
$cat_title = $row['cat_title'];
$cat_id = $row['cat_id'];

?>
<input value="<?php echo $cat_title; ?>" type="text" class="form-control" name="cat_title">
<?php
}
}

}


Related Topics



Leave a reply



Submit