Not able to Display without storing it in DB - php

I am not sure whether I am displaying the image in the right way or not. I am not able to display the image in the given field and the images are not stored in DB ...it's a gender based avatar choosing script (I hope so).
if (isset($_GET['gender']) == 'm') {
echo ' <img id="avat" src="imgs/avatar-boy-1.png" alt="">';
} elseif (isset($_GET['gender']) == 'f') {
echo ' <img id="avat" src="imgs/avatar-girl-1.png" alt="">';
}

Funk Forty Niner and Dharman pointed this out in the comments, but isset($_GET['gender'])=='m' doesn't make sense.
isset($_GET['gender']) returns a boolean, true or false, telling you whether $_GET['gender'] is set. You can't meaningfully compare that boolean to 'm'.
You probably want something like isset($_GET['gender']) && $_GET['gender'] == 'm'.
Or just move the isset($_GET['gender']) into its own if statement, since you are checking it both times.

Related

how to load specific files/urls based on a database query

I am trying to alter a piece of code that will allow me to position an image left, right or centred.
I have been able to create radio buttons that populate a table on the database but now I want to pull that information out and create an IF statement that will then open a specified page depending on the result.
For example, If I choose the 'right' radio button, this will then populate the database with the id and the 'right' value. I then want to pull the value from the database so if the value is 'right' the newsitem2.php page will load.
This is the code I have in order to do this:
if ("" . $tnrow['newsimage'] . "" == "none") {
}else {
$radio="SELECT imageposition FROM newsimage";
if ("$radio" == Centre) {
<p class='newsmore'> <a href='newsitem3.php?i=" . $tnrow['niid'] . "'>Read More</a></p>
}
if ("$radio" == Right) {
<p class='newsmore'> <a href='newsitem2.php?i=" . $tnrow['niid'] . "'>Read More</a></p>
}
if ("$radio" == Left) {
<p class='newsmore'> <a href='newsitem.php?i=" . $tnrow['niid'] . "'>Read More</a></p>
}
However, it is not loading each page it just loads the 'newsitem' page with the image on the left. is there any reason for this?
You don't make any call to your database, so $radio is just a String when you use it for now (representing the query) ; you have to send this query to your database (using for example a wrapper like PDO), and then get the result back.
Another thing, are Right, Left, ... constants or enum values ? 'Cause if they're not, you also have to put them into quotes, in order to be interpreted as String
There are a lot of weird instructions in your code, you should probably get more informations on PHP, 'cause you doesn't really seem to understand the concepts for now. Get a good tutorial/book, and you'll see that what you want to do is pretty easy when you got the ideas behind. ;)
Your IF's are all wrong. I assume the value of $radio is a string?
if ("$radio" == Centre) {
Should be
if ($radio == 'Centre') {
And so on for all the others

Empty value clause for a string not working

I am trying to get value with ajax and the values are in form of string, but the problem is when I'm trying to use a condition of if value empty then return this or else do that
My code is
if (empty($title) || empty($thumbnail) || empty($link))
{
echo "404";
}
else
{
some custom line
}
My problem is that a 404 is returned even if the value of any one of them (title, thumbnail, or link) is not empty. Can any one point me where I am wrong?
Here's how I am getting value from ajax:
$title = mysql_real_escape_string($_REQUEST['title']);
$thumbnail = mysql_real_escape_string($_REQUEST['thumbnail']);
$link = mysql_real_escape_string($_REQUEST['link']);
First of all I suggest you to do not use empty, cause it got some specific behave.
Second: I don't get why you are using mysql_real_escape_string, you have to query a database?
Moreover i guess you (as #havelock pointed out) wanted to get a 404 if none of that values are set up.
I'm for this:
if($title && $thumbnail && $link){
//all fine..
}
else 404

Displaying default image

I have a default image set in the db in the column default_image. If the default_image = 1 I want it to display the default image and if default_image = 0 I want text to say no image available.
How do I make sure only the default image is displayed. Here is what I am doing now and it just displays any image that has the same link_id.
the column media_link is the address to the image.
<?php if (!empty($row['default_image'])){
echo "<a href='http://localhost/images".$row['media_link']."'><img src='http://localhost/images".$row['media_link']."'/></a>";
}
else{
echo "<div align=\"center\">No image available</div>";
}?>
What do I do to make default_image "1" in the database display and nothing else? Thanks.
EDIT:
Here is another piece of code to show all the images for that link_id
if ($row['media_link']){
echo "<a href='http://localhost/t_images".$row['media_link']."'><img src='http://localhost/images".$row['media_link']."' /></a>";
}
else{
echo "<div align=\"center\">No images available</div>";
}
}
there is 6 images for this current id
When i echo the vardump on this page the correct image has this
string(1) "1"
while the other 5 images "non default"
show this string(1) "0"
I am not sure how to show only the correct default image where it goes on the page in my first code.
Does this info help at all?
EDIT AGAIN/FIXED:
I fixed it by adding ORDER BY default_image DESC to the end of my query. Don't know if that is the best thing to do but if anyone has any other suggestions i would be glad to hear them. Thanks everyone for all your help. You all gave great answers but in the end I just added the order by statement to my query and it works that way so I am happy.
Thanks!
In your situation, $row['default_image'] is a string. Of course, PHP is not a strongly typed language, but it still has to figure out what you mean. In your example, you are expecting this:
empty("0") === true
which does not happen. The empty string "" is empty, the number 0 is empty, but the string "0" is not. Instead you could use either of:
$row['default_image'] !== "0"
// Uses strict comparison, which might not be what you expect
0+$row['default_image'] != 0
// Uses casting, which might fail if the string doesn't contain a number
$row['default_image'] != "0" || !empty(0+$row['default_image'])
// Uses short-circuiting, and should cover all (most) cases
There are other variants. Myself, I am not sure which I would choose between them: it depends on what you mean, exactly.
Check Default_image against 1 instead of empty.
try: ..
<?php
if( isset($row['default_image']) && $row['default_image'] == 1){
echo "<a href='http://localhost/images".$row['media_link']."'><img src='http://localhost/images".$row['media_link']."'/></a>";
}else{
echo "<div align=\"center\">No image available</div>";
}
?>
try this ...
<?php if (!empty($row['default_image']) && $row['default_image'] == 1){
echo "<a href='http://localhost/images".$row['media_link']."'>
<img src='http://localhost/images".$row['media_link']."'/></a>";
}
else{
echo "<div align=\"center\">No image available</div>";
}?>

Using PHP $_SERVER[HTTP_REFERER] to display custom welcome messages/content

I'm trying to display a custom welcome message to visitors based off what social media site they are coming in from. I can't get this string to work but it is echoing back no matter what the refering site is. Can anyone help me get this to work? Many thanks!
<?php
if (strpos("twitter.com",$_SERVER[HTTP_REFERER])==0) {
echo "Welcome, Twitter User! If you enjoy this post, don't hesitate to retweet it to your followers";
}
?>
Tried using
<?php
if(isset($_SERVER['HTTP_REFERER'])) {
echo $_SERVER['HTTP_REFERER'];
}
?>
to get this to work but no luck.
strpos returns false when the search word does not appear in the string. 0 also evaluates to false when compared to a boolean. So false == 0, and your code always runs.
Use a strict comparison to require both value and type match instead of ==
if (strpos("twitter.com", $_SERVER['HTTP_REFERER']) === 0) {
echo "Welcome, Twitter User! If you enjoy this post, don't hesitate to retweet it to your followers";
}
However, the referrer will not start with twitter.com, it'll start with http:// or https:// so your condition wasn't right in the first place. To search for twitter.com anywhere else in the string:
if (strpos("twitter.com", $_SERVER['HTTP_REFERER']) !== false) {
echo "Welcome, Twitter User! If you enjoy this post, don't hesitate to retweet it to your followers";
}
I think you may have confused strpos() with strcmp(), being strcmp() returns 0 when two strings are equal. strpos() returns the position at which a string was found. Try:
<?php
if (strpos('twitter.com', $_SERVER['HTTP_REFERER'] != 0)) {
echo "Welcome, twitter user.";
}
?>

using or (||) in php if condition creates problem

i have done something like this:
<form action="validate.php" method="get">
Id :<input type="text" name="pID"/><br/><br/>
Name :<input type="text" name="pName"/><br/><br/>
Description :<input type="text" name="pDesc"/><br/><br/>
Price :<input type="text" name="pPrice"/><br/><br/>
<input type="submit" name="pSub"/>
</form>
my validate.php contains :
<?php
if (!empty($_GET['pID']) || !empty($_GET['pName']) || !empty($_GET['pDesc']) || !empty($_GET['pPrice'])){
if(is_numeric($_GET['pID']) || is_numeric($_GET['pPrice']))
{
echo "</br>Your ID :".$_GET["pID"]."</br>";
echo "Name is :".$_GET["pName"]."</br>";
echo "Description :".$_GET["pDesc"]."</br>";
echo "and Price :".$_GET["pPrice"]."</br>";
}
else{echo "Pls See That ID and Price are Numerical";}
}else{
echo "Fill up All The Values";
}
?>
is not working properly ,
1st if conditions doesn't work properly
ie. if i left blank "Name" input field message should have come saying
"Fill up All The Values "...... instead its showing the list of inputs
is there any other way to validate form (PHP)
You are using the wrong operator: || means "logical OR"; what you seem to be looking for is &&, that is "logical AND".
The code does exactly what you told it to do (see the documentation); the fact that you intended something else is not relevant to the computer:
if (!empty($_GET['pID']) || !empty($_GET['pName']) || !empty($_GET['pDesc']) || !empty($_GET['pPrice']))
means "if pID is not empty OR pName is not empty OR ..."; as soon as one or more of the fields are not empty, the condition evaluates to true.
What you can do to get what you meant:
replace OR with AND ( && )
use if (!(empty($_GET['pID']) || empty($_GET['pID'] ...)) - note that the whole expression is negated in parentheses
(read on De Morgan's laws to see why these two solutions are equivalent)
It's probably better that you switch the conditions around, like this:
if(empty($_GET['pID']) || empty($_GET['pName']) || empty($_GET['pDesc']) || empty($_GET['pPrice'])) {
echo "Please fill up all the values";
} else {
// Do other validation.
}
This way, you know your inputs are correct before you do anything else. Obviously I've not tested this, but it should work as expected. What you were saying before was asking if ANY of the inputs was not empty, do the additional validation. As one of the other commenters explained, if you wanted to do that, you should be using && rather than ||.
Changing it around just makes it a bit clearer!
It's a logic problem with your code. Using || in this situation means that if ANY of those inputs contains a value, then the first condition is met. What you want to do is AND, not OR, so that the first condition is only met if all of the inputs are !empty.
I don't remember for sure what the AND operator is for PHP, since it's been a long time, but it's probably &&.
The question already has been answered, but there is one more thing,
I recommend that you use $_POST instead of $_GET because $_POST is way more secure, as you use HTML Forms. You could look it up on internet.
Here is a link, the first answer says it all: Difference between $_POST & $_GET
This is just wrong
if (!empty($_GET['pID']) || !empty($_GET['pName']) || !empty($_GET['pDesc']) || !empty($_GET['pPrice'])){}
You need to make it like this:
if (!empty($_GET['pID'], $_GET['pName'], $_GET['pDesc'], $_GET['pPrice'])){}
And no need to make ORs at all. Also, you'd better check if any of the given values are empty and throw error on that.

Categories