Reputation: 71
I am very new to PHP and am trying to create a basic form. This form has a drop-down list whereby the product is displayed. What I would like for it to do is to display the price, and the city that this product is from.
I have a text file which acts as the data source, and is formatted in this way:
product | price | city
Code:
<?php
$file = 'Product1.txt';
$handle = @fopen($file, 'r');
if ($handle) {
while (!feof($handle)) {
$line = fgets($handle, 4096);
$item = explode('|', $line);
echo '<option value="' . $item[0] . '">' . $item[0]. ' '.$item[1]. ' '.$item[2].'</option>' . "\n";
}
fclose($handle);
}
?>
Overall, when I populate my dropdown list, it works fine, and I've extended the page with the hopes of getting more. However, when I try to echo $item[1]
or $item[2]
at the end of the page, I get nothing.
Can someone show me what I am missing? I've tried looking for tutorials on this matter but to no avail. Thank you.
<?php
echo "<h2>Your Input:</h2>";
echo $name;
echo "<br>";
echo $_POST['D1'];
echo "<br>";
echo $quantity;
?>
Upvotes: 0
Views: 112
Reputation: 3520
You are not getting anything because $item not in scope
put $itemData = array(); at the top after $handle
$file = 'Product1.txt';
$handle = @fopen($file, 'r');
$itemData = array();
if ($handle) {
while (!feof($handle)) {
$itemData[] = explode('|', $line);
$line = fgets($handle, 4096);
$item = explode('|', $line);
echo '<option value="' . $item[0] . '">' . $item[0]. ' '.$item[1]. ' '.$item[2].'</option>' . "\n";
}
fclose($handle);
}
its should work
Upvotes: 1