How to use the result of mysql_fetch_array? - php

I'm using a while statement on this and I can echo each row fine i.e
echo $row['myrow'];
but what I want is to have the result put into a link like so:
echo "<img src='http://www.mysite.com/images/$row['myrow'].jpg'>";
But it doesn't work. What am I doing wrong?

Either echo it this way:
echo "<img src='http://www.mysite.com/images/{$row['myrow']}.jpg'>";
Or, IMHO much better, this way:
echo "<img src='http://www.mysite.com/images/".$row['myrow'].".jpg'>";
Give the documentation on double quoted-strings a quick refresh.

Another nice way to do it is to only use PHP for the dynamic part of the code. I think it results in nicer looking code.
<img src="http://www.mysite.com/images/<?php echo $row['myrow']; ?>.jpg">
Then of course the whole img tag should not be in a PHP code block, since it regular HTML.

You need to take care of your quotes...
Try this:
echo '<img src="http://www.mysite.com/images/'.$row['myrow'].'.jpg" />';
Also notice that you didn't close the element.

Accessing array elements and object properties/methods inside the string must be enclosed in curly braces (string parsing)
echo "<img src='http://www.mysite.com/images/{$row['myrow']}.jpg'>";

to make it complete, lol
echo "<img src='http://www.mysite.com/images/$row[myrow].jpg'>";

you have write your site url on place of example.com
echo "<img src='example.com/images/'.$row['myrow'].'.jpg'>";

Related

How to concatenate variable with anchor's href

I'm noob in php, and i'm trying to concatenate href with words or values ex:
$name = "anonymous";
echo ''.$name.'<br/>';
I want it to look like this when the user clicked the link.
echo ''.$name.'<br/>';
I've tried doing this, but it's not working.
echo ''.$name.'<br/>';
echo ''.$name.'<br/>';
The same way you concatenated the text.
You cant use php tags inside a php tag like this,
echo ''.$name.'<br/>';
It will mess the PHP interpreter so use this
echo "<a href=profile.php/{$name}>".$name."</a><br/>";

How can I fix this HREF issue

I am struggling with an issue which I need a little bit of help with, basically I have created a search bar and when the results come up I only want the 'Session 1' or 'Session 2' to be href'd. Not the whole thing. A photo below:
img http://gyazo.com/eb677171c15b075f1fb4137d28227b3a.png
I would just like the Sessions to be hrefed but the paragraphs just.
My code below:
echo "<p><a href='../pages/session.php</a>".$results['1']."'><h3>".$results['title']."</h3>".$results['text']."</‌​p>";
Linking to my DB
Note that is not all of my code just where I believe the issue lies.
You placed </a> slightly wrong. The correct solution would be like this:
echo "<p><a href='../pages/session.php'>{$results['1']}</a><h3>{$results['title']}</h3>{$results['text']}</‌​p>";
If you use double quotes, you do not need to concatenate variables into your string, they are also validate within the string.
UPDATE
To display only the headlines as links try this:
echo "<a href='../pages/session.php'><h3>{$results['title']}</h3></a><p>{$results['text']}</‌​p>";
You have missed a bracket in ur a tag.It is like <a href='whatever'>blah</a>
Do you just want to href $results['1']? Here you go.
echo "<p><a href='../pages/session.php'>".$results['1']."</a><h3>".$results['title']."</h3>".$results['text']."</‌​p>";

Displaying an image from a directory in PHP

I'm trying to display an image file from a directory using a PHP echo command and an IMG tag.
Here is the code:
//These variables represent the file name extensions from a form element from a previous page
$bannerimg=$_POST["banimg"];
$adimage=$_POST["adimage"];
echo "<img src='imgdir/'".$bannerimg."/>";
When I echo out the file variables ($bannerimg and $adimage) I get the proper file name and extension.
In theory, will this work? If so, what is the proper syntax to handle that echo statement?
Thanks for all the help.
Dustin
Yes it would work, but you should have tested that already.
Alternative syntaxes to the echo statement that I find a little bit more readable would be:
echo "<img src='imgdir/{$bannerimg}/>";
echo "<img src='imgdir/$bannerimg/>";
You can read all about variable parsing in the manual, the first syntax is the complex one and the second the simple. I prefer the complex one as the end of the variable is clearly defined and you can use it for complex expressions, not just simple variables.
You're doing it right but you can use the following just to keep it clean.
echo "<img src='imgdir/$bannerimg' />";
It should work.
To Sandeep's comment, I would have gone the other way.
echo '<img src="imgdir/'.$bannerimg.'/>';
Using " means the parser needs to check to see if there is anything to evaluate.

Generating a random number and adding to an HTML argument's string

I'm trying to make a header image change randomly based on a random number being chosen. This is the code I have right now, and it requires the entire tag.
<img src="http://www.example.com/site_gfx/headers/header_<?php echo(rand(1,7)); ?>.png" width="980" height="230" alt="Example Site" />
Is there any reason it would be dying like it is? Where the <?php echo part is is the PHP code i'm using to generate the random number, and I'd like to include that into the string for the img src
How's it dying? I'd try print or echo without parentheses, as I haven't seen echo() used before:
print():
<?php print(rand(1,7)); ?>
echo:
<?php echo rand(1,7); ?>
Figured it out. The problem is I didn't realize the <?php area was running within an echo command (stupid little oversight on my part). But modifying the echo statement that I was working with fixed it.
Thanks for the pointers, everyone.
Any reason you want to do this with PHP, it would probably be easier with javascript. try to remove the rand(1,7) from the bracket so as to have:
echo rand(1,7)
This would be a better approach to doing this
<?php
$number = rand(1,7);
echo '<img src="http://www.example.com/site_gfx/headers/header_' . $number . 'png" width="980" height="230" alt="Example Site" />'
?>
There may be an issue with using the <?php tag inside of another tag.

Show an Image if a PHP argument proves true

I barely know how to use PHP and I can't seem to make my code show an image if a condition proves true. This is the code:
<?php
$search=get_search_query();
$first=$search[0];
if ($first=="#"){
}
?>
I tried writing this thinking it would work and it didn't:
echo "<html>";
echo "<img src='http://chusmix.com/Imagenes/grupos/lujan.jpg'>";
Also I tried a code I found which started with the function: header() but it caused a tremendously long error, which said something like header already defined.
Thanks
You have used 'double quotes' incorrectly in the echo statement.
Try the following:
echo "<img src='http://chusmix.com/Imagenes/grupos/lujan.jpg' alt='Preview not available' />"
Regards,
Mahendra Liya.
You should var_dump($first) to know what it contains
check if the condition is really getting true
and also put single quote inside the double quote.
if ($first=="#"){
echo 'yes it is true';
echo "<img src='http://chusmix.com/Imagenes/grupos/lujan.jpg'>";
}
close the img tag
The part of the query string starting with # (so-called "hash") is not being sent to the server. That is, if your page is called like myblog.com/foo?bar=baz#quux, you php script will only receive myblog.com/foo?bar=baz. You need javascript if you want to handle urls with hashes.

Categories