This is a really bizarre issue that I've been having.
It happens to images that seem to have a name that is similar to something else.
For instance, if I have an image named image0001.png and I try to display an image with the source of image0010.png - which doesn't exist - then the image does not display as nothing, or fire onerror image, instead it displays as image0001.png which does exist.
I have no idea how to to fix this because I don't really know what is going on.
I am using the following code to fetch results from the database and produce a table with item details.
<?php while ($row = $retval->fetch_array()) { ?>
<tr>
<td>
<?= $row['id']; ?>
</td>
<td>{$row['name']}</td>
<td>
<a href="/path/to/item/<?= $row['id']; ?>.php">
<img src="/images/<?= $row['id']; ?>.jpg" onerror="this.src='/images/error.png';">
</a>
</td>
<td><?= $row['description']; ?></td>
</tr>
<?php } ?>
Sounds like mod_speling is enabled on your server.
Requests to documents sometimes cannot be served by the core apache server because the request was misspelled or miscapitalized. This module addresses this problem by trying to find a matching document, even after all other modules gave up.
Documentation
You can try to disable this apache module in a .htaccess file:
CheckSpelling Off
The obvious other alternative is to use file_exists, removing the reliance of JavaScript and ensuring that the image is shown if it exists or an error image if not.
<?php while ($row = $retval->fetch_array()) { ?>
<tr>
<td>
<a href='/path/to/item/<?= $row['id']; ?>.php'><?= $row['id']; ?></a>
</td>
<td>{$row['name']}</td>
<td>
<a href='/path/to/item/<?= $row['id']; ?>.php'>
<?php if (file_exists("/images/{$row['id']}.jpg")) { ?>
<img src="/images/<?= $row['id']; ?>.jpg" />
<?php } else { ?>
<img src="/images/error.png" />
<?php } ?>
</a>
</td>
<td><?= $row['description']; ?></td>
</tr>
<?php } ?>
Related
<?php
$pros_array_id= array_column($_SESSION['cart'], 'paint_id');
$productee = $productn->getData('paint');
foreach($productee as $pro):
if($pro['paint_id'] == $pros_array_id):
?>
<table>
<tr>
<td class="imgTag">
<img class="img-fluid" src="<?php echo $pro['paint_image'] ?? 1; ?>" >
</td>
</tr>
</table>
<?php
endif;
endforeach;
?>
Am trying to display session cart items and its not showing anything. When I print_r($productee) and print_r($pros_array_id) after the foreach statement both display the accurate data, yet nothing displat in the <tr> tag.
The is to display the result
When I implode $pros_array_id like this "$imp = implode(" ",$pros_array_id);" and put the variable in the if-statement, it works fine if only one product is in the session, but the moment I add more than one products in the session, nothing is display again.
Please can someone point to me what I should do?
Thanks
Try it like this:
<table>
<?php
$pros_array_id = array_column($_SESSION['cart'], 'paint_id');
$productee = $productn->getData('paint');
foreach($productee as $pro) {
//Also see and try to disable this check to see if it works then!
if($pro['paint_id'] == $pros_array_id) { ?>
<tr>
<td class="imgTag">
<img class="img-fluid" src="<?php echo $pro['paint_image'] ?? 1; ?>">
</td>
</tr>
<?php
}
}
?>
</table>
Always make sure while debugging this kind of code to eliminate any checks that could result in false, so try disabling the "if clause" as well, to see if it displays something then.
Have a problem that I do not know how to solve.
Has a database with a couple of thousand documents pdf, mp4, mp3 etc ..
The search works well until I come across a file that is missing, because something went wrong when uploading, the file name is wrong in the database etc .. simply the file is missing even if it should be in the database.
So when i do a search the result came up but when i click. But i dont get the file and end upp in nowhere.
How can I get a good handle on this. Would like to be able to get a text where it says that the file does not actually exist because something has occurred.
The code
<?php while ($row = $sqlanswer->fetch()): ?>
<tr>
<td> <a href="<?php echo "../", ($row['doc_type']),"/",($row['doc_main_folder']),"/",($row['doc_sub_folder']),"/",($row['doc_file_name']); ?>" ><?php echo ($row['doc_name']); ?> </a><br></td>
<td><?php echo ($row['name']); ?><br></td>
<td><?php echo ($row['doc_type']); ?><br></td>
</tr>
<?php endwhile; ?>
It's not 100% clear what exactly you're doing
In general it sounds like you'd want to add
<?php if file_exists(<path_to_your_file>){ ?>
<tr>
<td> <a href="<?php echo "../", ($row['doc_type']),"/",($row['doc_main_folder']),"/",($row['doc_sub_folder']),"/",($row['doc_file_name']); ?>" ><?php echo ($row['doc_name']); ?> </a><br></td>
<td><?php echo ($row['name']); ?><br></td>
<td><?php echo ($row['doc_type']); ?><br></td>
</tr>
<?php } >
trying to get my head around REST, I am following/copying a tutorial. The "$_get" is blank, I noticed that the URL that is being called is blank here is a copy,
http://localhost/RestClient/index.php?action=get_user&id=
but the href I am clicking looks ok to me.
<a href='http://localhost/RestClient/index.php?action=get_user&id='3' alt=user_'3'>Carbonnel</a>
here is my code I am new to PHP so figuring it all as I go!!!!
<?php
/*** this is the client ***/
if (isset($_GET["action"]) && isset($_GET["id"]) && $_GET["action"] == "get_user") // if the get parameter action is get_user and if the id is set, call the api to get the user information
{
$user_info = file_get_contents('http://localhost/RestServer/api.php?action=get_user&id=' . $_GET ["id"]);
$user_info = json_decode($user_info, true);
// THAT IS VERY QUICK AND DIRTY !!!!!
?>
<table>
<tr>
<td>Name: </td><td> <?php echo $user_info["last_name"] ?></td>
</tr>
<tr>
<td>First Name: </td><td> <?php echo $user_info["first_name"] ?></td>
</tr>
<tr>
<td>Age: </td><td> <?php echo $user_info["age"] ?></td>
</tr>
</table>
<a href="http://localhost/RestClient/index.php?action=get_userlist" >Return to the user list</a>
<?php
}
else // else take the user list
{
$user_list = file_get_contents('http://localhost/RestServer/api.php?action=get_user_list');
$user_list = json_decode($user_list, true);
// THAT IS VERY QUICK AND DIRTY !!!!!
?>
<ul>
<?php foreach ($user_list as $user): ?>
<li>
<?php echo "<a href='http://localhost/RestClient/index.php?action=get_user&id='".$user ['id']."' alt=user_'".$user['id']."'>"; ?><?php echo $user["name"] . "</a>"; ?>
</li>
<?php endforeach; ?>
</ul>
<?php
}
?>
The Link
<a href='http://localhost/RestClient/index.php?action=get_user&id='3' alt=user_'3'>Carbonnel</a>
is incorrect, it must be:
<a href='http://localhost/RestClient/index.php?action=get_user&id=3' alt='user_3'>Carbonnel</a>
Watch the changes in ' signs.
In you example $_GET['id'] must have been always null.
There is definitely something wrong with you <a>.
You are using single quotes for the tag attribute and then for query string parameters too.
Any program having to interpret that will have no idea where the href= actually ends.
One solution would be to use double quotes (") for the attribute and single quotes for the value (if you need those at all).
Change
<?php echo "<a href='http://localhost/RestClient/index.php?action=get_user&id='".$user ['id']."' alt=user_'".$user['id']."'>"; ?>
to
<?php echo "<a href='http://localhost/RestClient/index.php?action=get_user&id=".$user ['id']." alt=user_".$user['id']."'>"; ?>
I've always wondered: does having a large open and close PHP (i.e. a template) hurt?
To clarify:
<html>
<?php echo $test; ?>
<body <?php echo $test2; ?>>
<table>
<?php foreach ($rows as $row): ?>
<tr>
<td><?php echo $row['cell1']; ?></td>
<td><?php echo $row['cell1']; ?></td>
<td><?php echo $row['cell1']; ?></td>
<td><?php echo $row['cell1']; ?></td>
<?php echo $row['added cells']; ?>
</tr>
<?php endforeach; ?>
</table>
<?php echo $someMorePhp; ?>
<div>
<?php /*do some more php stuff */ ?>
</div>
etc etc
etc
Or would it be advisable to i.e.
<html>
<php echo $test.'<body'.$test2; ?>>
<table>
<?php foreach ($rows as $row) {
echo '<tr>
<td>'.$row['cell1'].'</td>
<td>'.$row['cell2'].'</td>
<td>'.$row['cell3'].'</td>
<td>'.$row['cell4'].'</td>
<td>'.$row['cell1'].'</td>
'.$row['added cells'].'
<tr>';
}
</table>
// etc
I know this might seem like a micro optimization and such. To be clear i'm looking for a rule-of-thumb not a specific usecase... Will it hurt entering and exiting the php engine during a single script run...
No, that is no problem, you should always opt for better readability in this case.
Also think about either activating shorttags if possible (but beware, those might have some sideeffects / problems with XML sytnax as stated in the comments)
<? if ($bool) { ?>
<?= $myVarThatIWantToOutput ?>
<? } ?>
Or think about using a template engine like smarty or twig, which restrict you somehow to force splitting of concerns, make some stuff easier (and more readable) and allow caching of compiled templates to make sure you still get the right speed.
<?php
$xml = simplexml_load_file('http://www.google.com/ig/api?weather=London');
$information = $xml->xpath("/xml_api_reply/weather/forecast_information");
$current = $xml->xpath("/xml_api_reply/weather/current_conditions");
$forecast_list = $xml->xpath("/xml_api_reply/weather/forecast_conditions");
?>
<html>
<head>
<title>Google Weather API</title>
</head>
<body>
<h1><?php print $information[0]->city['data']; ?></h1>
<h2>Today's weather</h2>
<div class="weather">
<img src="<?php echo 'http://www.google.com' . $current[0]->icon['data']?>" alt="weather"?>
<span class="condition">
<?php echo round(conver_f_c($current[0]->temp_f['data'])); ?>° C,
<?php echo $current[0]->condition['data'] ?>
</span>
</div>
<h2>Forecast</h2>
<?php foreach ($forecast_list as $forecast) : ?>
<div class="weather">
<img src="<?php echo 'http://www.google.com' . $forecast->icon['data']?>" alt="weather"?>
<div><?php echo $forecast->day_of_week['data']; ?></div>
<span class="condition">
<?php echo round(conver_f_c($forecast->low['data'])); ?>° C - <?php echo round(conver_f_c($forecast->high['data'])); ?>° C,
<?php echo $forecast->condition['data'] ?>
</span>
</div>
<?php endforeach ?>
</body>
</html>
<?php
function conver_f_c($F){
return $C = ($F − 32) * 5/9;
}
I want Out somthing like this manner of the horizontal ,
Even i tried UL LI WITH display inline but it goes failed,
Tell me some good suggestion for my probme,
I want exactly like horizontal, expecting exactly like screen shot ,
Tell me How to render using php
Thanks
alt text http://img163.imageshack.us/img163/7518/weatherhori.jpg
Above snippet present display out verticly , i want o change that verticle to horizonatal ,
somthing like this screen shot
<table>...</table>
Update
From your latest comment so far:
i know how to fetch array and display
it, but i dont know to fetch and
display in the verticl manner that is
the stuck up
I feel this is going to be a stupid answer but it appears to be what you don't understand...
The web is based in a markup language called HTML. This language has tags (delimited by angle-brackets) that allow you to define the structure of a document. On top of this, you have another language called CSS. This other lang allow you to define how HTML is going to be displayed on screen.
You may argue that you already have a web page and you've written it with the PHP language instead of the two other langs I've mentioned. That's not enterely true: you code in PHP, sure, but you use PHP to generate HTML. And it's HTML what finally reaches the browser (Firefox, Explorer...). PHP is executed in the web server, not in the browser. The browser can only see whatever HTML you've generated.
To sum up: you have to forget about PHP, Google and the whole weather thingy. You first need to write a static HTML document and style it with CSS. Once you've done with it, you can finally replace the parts of the information that are dynamic with values taken from your PHP variables.
And since you seem to need a table to display tabular data, the appropriate HTML tag is <table>:
<table>
<tr>
<th>Web</th>
<th>Thu</th>
<th>Fri</th>
<th>Sat</th>
</tr>
<tr>
<td><img src="/path/to/pics/cloudy.png" width="25" height="25" alt="Cloudy"></td>
<td><img src="/path/to/pics/sunny.png" width="25" height="25" alt="Sunny"></td>
<td><img src="/path/to/pics/rainy.png" width="25" height="25" alt="Rainy"></td>
<td><img src="/path/to/pics/cloudy.png" width="25" height="25" alt="Cloudy"></td>
</tr>
<tr>
<td>26ºC</td>
<td>26ºC</td>
<td>22ºC</td>
<td>25ºC</td>
</tr>
<table>
I suggest you find some tutorials about basic HTML and CSS. They'll be of invaluable help.
This is what's done by Google :
http://jsfiddle.net/bW8NA/1