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

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.

Related

place html inside php code the right way and that works

i am trying to make html works and the right way to place it so it works placing html inside php code
<?php echo <td class=\"network\">".$show."</td>;?>
This does not give the impression that you've really tried much, or researched the issue.
<?php echo "<td class=\"network\">".$show."</td>";?>
^^ Should work to output HTML tags. (Though invalid as td should exist inside tables.)
Also note you can use different quotes to reduce the need for escaping. E.g.
<?php echo '<td class="network">'.$show.'</td>';?>

Target in Echoed php link

I have a problem I can't solve by myself, don't know how simple it is to solve but I want to open this link in another frame:
<?php echo "<a href=details.php?id=$row[idProdukt]>$row[Produkt_Namn]</a>" ?>
tried to use taget but since it's html I couldn't really wrap my head around how to type it.
any help is greatly appreciated.
Just add the attribute to your anchor tag as usual. Make sure you use the name of the targeted iframe as its value:
<?php echo "<a href='details.php?id=$row[idProdukt]' target='framename'>$row[Produkt_Namn]</a>" ?>
I added quotes around your html attribute values as that is a good practice to be in. It prevents issues arising from errant or intentional spaces in attribute values.
First, you have error on array index, missing quotes. You can use this;
<iframe src="some_url" name="test">
.....
</iframe>
<?php echo "" . $row['Produkt_Namn'] . ""; ?>
The trouble is missing quotes around href attribute value and array key.
echo "{$row['Produkt_Namn']}";
See in action

php echo working html code from mysql?

I have a chunk of html code stored in a field in a mysql database. If i try and echo it out it just sits on the page as text. Is there a way to echo it out in a working fashion. ( Meaning the html displays what it is meant to display )
The code is stored in a varchar field and i used nl2br with special characters to insert it. The code display's as text but it is what it should be.
Again can i echo the code out in a working way?
EDIT - This is the code that is displayed on the page.
<img name="" src="http://www.mysite.com/assets/viewcart.png" width="114" height="48"alt="" />
and this is the code stored in the database.
<img name="" src="http://www.mysite.com/assets/viewcart.png" width="114" height="48" alt="" />
The Echo request i used to get the code out was a simple.
echo $row['htmlcode'];
Thanks for any help and examples.
You could try running your text through html_entity_decode() before echoing it.
as in echo html_entity_decode($row['htmlcode']);

How to use the result of mysql_fetch_array?

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'>";

aligning the name<p align="left">

i wanted to align my Welcome note to the right by using <p align="right">
but it doesnt seem to work..is it because Session/PHP doesnt work with p align?
<?php
session_start();
if($_SESSION['SESS_admin'] == 0)
require("do_menu.php");
else
require("do_menu3.php");
require("auth.php");
require("do_html_header.php");
do_html_header();
print"<p align=\"RIGHT\"><h1>Welcome ". $_SESSION['SESS_FIRST_NAME']."!</h1></p>";
do_menu();
?>
thanks in advance!
Try this
echo "<div style='text-align:right'><h1>Welcome ". $_SESSION['SESS_FIRST_NAME']."!</h1></div>";
It is probably because your <h1> tag is not set to align right and is instead set to align center. You need to override this with CSS. Try this instead: echo "<h1 style="text-align: right;">Welcome ".$_SESSION['SESS_FIRST_NAME']."</h1>"
print"<p align=\"RIGHT\"><h1>Welcome ". $_SESSION['SESS_FIRST_NAME']."!</h1></p>";
You're allowed to use single qoutes, too. It might help you here.
echo '<p align="right"><h1>Welcome '. $_SESSION['SESS_FIRST_NAME'] .'!</h1></p>';
Also, print should have a space after it, or you should be wrapping what's being printed in parenthesis.
Edit: I used echo out of habit, sorry. You can use either, but I [think] I read somewhere that echo is a better choice than print - not 100% sure, though, but they do the same thing. I'll go look it up, though. (Of course, if anyone reading this knows if one is better than the other, let me know!)
The use of ALIGN is deprecated, and fails in many situations. In your case, you're probably writing something in the html_header.php which causes ALIGN to fail.
Try doing:
print "<div style=\"float:right\"><p><h1>Welcome ". $_SESSION['SESS_FIRST_NAME']."!</h1></p></div>";
<div align =right>
<?php
..
..
.. // code you want to align to the right
..
?>
</div>
This way you can align a particular segment of code to the right.
If later you want to align some other segment of code in the center.
Then you can use
This way you can align the different segments differently.

Categories