i have a problem with my PHP code, i've trying to put some information i get from a database into the URL so later on i can use $_GET.
The problem is use this code:
foreach ($get_wh_list as $key => $value)
{
echo $value['id'] . "<br />";
echo "<a href='?action=hot&&id='" . $value['id'] . "'>" . $value['wh_title'] . "</a><br />";
}
Right so as you can see i'm printing the id value, when i run this page the id shows up, but when i click a link the id doesn't appear in the URL. any ideas? I really can't think of why for the life of me.
Thanks for the help.
Remove the extra "&"
foreach ($get_wh_list as $key => $value)
{
echo $value['id'] . "<br />";
echo "<a href='?action=hot&id='" . $value['id'] . "'>" . $value['wh_title'] . "</a><br />";
}
Maybe it is better to use a tested and working function for that stuff
Use http_build_query(); to Serialize an Array to Query String
Put only one '&' in url instead of two '&&'
Example :
echo "<a href='?action=hot&id='" . $value['id'] . "'>" . $value['wh_title'] . "</a><br />";
Related
I'm working on a script that will retrieve API information of the 'near earth objects' from NASA's website. User selects date, api grabs the information and displays it. How do I fix the foreach in this script? would appreciate some help with this.
$jsonAsteroids = file_get_contents("https://api.nasa.gov/neo/rest/v1/feed?start_date=2018-08-01&end_date=2018-08-04&api_key=NNKOjkoul8n1CH18TWA9gwngW1s1SmjESPjNoUFo");
$data = json_decode($jsonAsteroids, true);
echo "<h4>Retrieving the first element (i.e. \"links\") of the JSON structure</h4>";
var_dump( $data["links"]);
echo "<h4>Retrieving the first element (i.e. \"next\") inside the \"links\" element</h4>";
echo( $data["links"]["next"]);
You were very close. Your main issue was that you used json_decode(..., true); which gives you an array, but then used the object->property syntax instead of object['property']. My suggestion is to use json_decode without the 2nd argument in this case.
Finally, your 2nd foreach was malformed.
<?php
$result = file_get_contents("https://api.nasa.gov/neo/rest/v1/feed?start_date=2018-08-01&end_date=2018-08-04&api_key=NNKOjkoul8n1CH18TWA9gwngW1s1SmjESPjNoUFo");
$data = json_decode($result);
foreach ($data->near_earth_objects as $date => $objects) {
echo "<p>" . count($objects) . " objects detected on $date</p>";
echo "<ol>";
foreach ($objects as $object) {
echo "<li>" . $object->name . " <a href='" . $object->nasa_jpl_url . "'>" . $object->nasa_jpl_url . "</a><br>";
echo "Diameter of the object: " . $object->estimated_diameter->meters->estimated_diameter_min . "-" . $object->estimated_diameter->meters->estimated_diameter_max . " metres<br>";
echo "<ul>";
foreach ($object->close_approach_data as $close_approach) {
echo "<li>Close approach: " . $close_approach->close_approach_date . " traveling at a velocity of " . $close_approach->relative_velocity->kilometers_per_hour . " km/h " . "missing " . $close_approach->orbiting_body . " by " . $close_approach->miss_distance->kilometers . " km</li> ";
}
echo "</ul>";
}
echo "</ol>";
}
I have been working on this for awhile and I am maybe making this more difficult than it is. Any shared knowledge would be very much appreciated.
My code calls data using an API, I was able to display it as a ordered list but I wanted to make it more presentable using a table format.
Ideally I the results to be displayed grouped by day.
Below is what I have created so far, but it is not outputting anything currently:
echo "<table>";
echo "<tr>";
echo "<th>Date</th>";
echo "<th>Number of Asteroids</th>";
echo "</tr>";
echo "<tr>";
foreach ($near_earth_object->close_approach_data as $close_approach) {
echo "<td>" . $close_approach->close_approach_date . "</td>";
foreach ($data->near_earth_objects as $date => $count) {
echo"<td>" . sizeof($count) . "</td></tr>";
}
}
echo "<tr>";
echo "<th>Asteroid Name</th>";
echo "<th>Asteroid Size</th>";
echo "<th>Velocity</th>";
echo "<th>Passing Earth By</th>";
echo "</tr>";
echo "<tr>";
foreach ($data->near_earth_objects->$date as $near_earth_object) {
echo "<td>" . $near_earth_object->name . " <a href='" . $near_earth_object->nasa_jpl_url . "'>" . $near_earth_object->nasa_jpl_url . "</td>";
echo "<td>" . $near_earth_object->estimated_diameter->meters->estimated_diameter_min . " metres</td>";
}
foreach ($near_earth_object->close_approach_data as $close_approach) {
echo "<td>" . $close_approach->relative_velocity->kilometers_per_hour . " km/h " . "</td><td>". $close_approach->miss_distance->kilometers . " km</td></tr> ";
}
echo"</table>";
You should avoid echoing HTML code whenever possible. You can use PHP Short tags <?= and ?> to inject PHP into your HTML code. This line
echo "<td>" . $close_approach->close_approach_date . "</td>";
can then be changed to
<td><?=$close_approach->close_approach_date?></td>
Furthermore I don't see any PHP opening and closing tags (<?php and ?>) to start and end your PHP code (although this may just be lacking in your example)
You're only looping through your table data <td> tags not your table row <tr> tags. You have to include these in your loop to keep creating a new table rows for every item in your loop:
foreach ($near_earth_object->close_approach_data as $close_approach) {
echo "<tr>";
Finally, PHP offers an alternative syntax for control structures. Check out the PHP documentation
PHP offers an alternative syntax for some of its control structures; namely, if, while, for, foreach, and switch. In each case, the basic form of the alternate syntax is to change the opening brace to a colon (:) and the closing brace to endif;, endwhile;, endfor;, endforeach;, or endswitch;, respectively.
You can use this to improve your existing code:
foreach ($data->near_earth_objects->$date as $near_earth_object) {
echo "<td>" . $near_earth_object->name . " <a href='" . $near_earth_object->nasa_jpl_url . "'>" . $near_earth_object->nasa_jpl_url . "</td>";
echo "<td>" . $near_earth_object->estimated_diameter->meters->estimated_diameter_min . " metres</td>";
}
And turn it into something like this
foreach ($data->near_earth_objects->$date as $near_earth_object): ?>
<td><?=$near_earth_object->name?> <a href='<?=$near_earth_object->nasa_jpl_url?>'><?=$near_earth_object->nasa_jpl_url?></td>
<td><?=$near_earth_object->estimated_diameter->meters->estimated_diameter_min?> metres</td>
<? endforeach; ?>
I recommend creating the table as you'd like to see it just with HTML and then using PHP to dynamically populate it. What may help is, rather than having PHP echo all of the HTML, just use PHP to populate the data (i.e., separate data from display). Just as an example taken from here:
<?php foreach ($things as $thing): ?>
<li><?php echo $thing; ?></li>
<?php endforeach; ?>
I was wondering if someone could help me. I want to iterate through an associative array and put the results into two combo boxes. The combo boxes will have the exact same data in each other.
while ($this->results = $this->dbhandle->fetch(PDO::Fetch_Assoc))
{
echo "<option value='" . $this->result['id] . "'>"
. $this->result['name'] . "</option>";
}
Do I have to run this loop twice for 2 seperate comboboxes or is there a more efficient way of doing this?
Your best bet in this situation is to just accumulate the text in a string and echo it out twice.
$options = "";
while ($this->results = $this->dbhandle->fetch(PDO::Fetch_Assoc))
{
$options.= "<option value='" . $this->result['id] . "'>"
. $this->result['name'] . "</option>";
}
echo "<select name='combo1'>".$options."</select>";
echo "<select name='combo2'>".$options."</select>";
How about something like this (raw code to give you an idea):
while
{
$combo_options .= "<option>" ....// rest of your code
}
Then print the variable inside your selects:
<select id="combo1"><?=$combo_options?></select>
<select id="combo2"><?=$combo_options?></select>
You can capture the output as a variable and then echo it as needed
while ($this->results = $this->dbhandle->fetch(PDO::FetchAssoc)){
$output .= "<option value='" . $this->result['id'] . "'>" . $this->result['name'] . "</option>";
}
/// later on in your script
print $output;
I'm trying to set variables inside a foreach() statement, but it keeps dying.
If I do this, all is fine.
foreach($array as $key => $value)
{
echo '<tr>';
echo '<td>' . $value['1'] . '</td>';
echo '</tr>';
}
But when I do this, it doesn't want to work.
foreach($array as $key => $value)
{
$mls = echo '' . $value['1'] . '';
echo '<tr>';
echo '<td>' $mls '</td>';
echo '</tr>';
}
Syntax wise, I don't see how there's a difference in these statements. I've also tried $mls = $value['1']; and that didn't want to work either.
Surely you got a syntax error complaining about the second case, right? If you say "it keeps dying", you should tell us exactly what happens when something dies. Even more, you should read the syntax error and consider what it says. The errors are descriptive so that you can figure out what's wrong.
In the second case, you aren't concatenating the strings with the . operator.
echo '<td>' $mls '</td>';
should be
echo '<td>' . $mls . '</td>';
$mls = echo '' . $value['1'] . '';
should be
$mls = $value['1'] ;
echo $mls;
and
echo '<td>' $mls '</td>'
should be
echo '<td>' . $mls . '</td>';
Your second code block should look more like this:
foreach($array as $key => $value)
{
$mls = (string) $value['1'];
echo '<tr>';
echo '<td>' , $mls , '</td>';
echo '</tr>';
}
When you type $var = echo "something" you aren't assigning any values to that variable. Instead you are outputting that string - echo has no return value.
You can typecast your variable into a string without appending and prepending empty strings.
You can use the , to echo multiple strings one after another with a little less overhead.
You should be using HTML entities for your ampersands, even though they are in an attribute's value
(Finally) You aren't actually concatenating your variable into the third echo.
I am really new to PHP programming, and I'm frustrated because I think this should work and it doesn't. I'm really missing something...
<?php
foreach ($datas as $name)
{
if ($name['state'] === 'MA')
{
echo
'<input type="hidden" name="id" value="' . $name['id'] . '" />' .
'<h2>' . htmlentities($name['name']) . '</h2>' .
'<p>' .
htmlentities($name['description']) . ' ' .
...
'<h1>' . $name['id'] . '</h1>';
foreach ($commentdatas as $name2)
{
if ($name['id'] == $name2['parkid'])
{
echo $name['id'] . $name2['parkid'];
}
}
}
}
?>
</div>
Everything worked well until I got to the second foreach statement. The foreach works. When I test, $name['id'] echos properly, as does $name2['parkid'].
There doesn't seem to be a problem with the system correctly identifying these, even in the loop. But inside the if statement, nothing echos at all.
Obviously something is wrong with the if statement. I've looked everywhere I can find and I'm having trouble figuring out the proper way to compare these variables. Can anyone help?
Try in_array() function
foreach ($commentdatas as $name2) {
if ($name['id'] == $name2['parkid'])
{
echo $name['id'] . $name2['parkid'];
}
}
}
replace with
if(in_array($name['id'],$commentdatas)){
echo $name['id'] . $name2['parkid'];
}