The following is my PHP code:
$heads = array();
$heads[0] = "<p class='ourmoto'>Your camera solutions...</p>";
$heads[1] = "<a href='index.html'><img id='rentals' src='images/rentalsnew2.jpg' alt='GroupLogo' /></a>";
$heads[2] = "<p class='ourmoto'>...Your camera solutions</p>";
for ($i = 0; $i<3; $i++){
echo"<p>$heads[$i]</p>";
}
CSS:
.ourmoto {
font-style:oblique;
font-size:30px;
display:inline;
margin: 0 auto;
}
From my knowledge, echo should print whatever comes after it in a quote as a string, even if its HTML tags. It manages to print the HTML but CSS won't apply on it. What am I doing wrong?
Thank you!
Iterate through each heads, echo paragraph with class:
foreach ($heads as $head)
echo "<p class='ourmoto'>{$head}</p>";
}
Related
I have been working on trying to retrieve specific results from a PHP for loop without success.
My objective is to run a loop on an array and to pull the specific value each time if it exists within the array. I am able to run the first loop and retrieve a value, however, when I run the second for function it retrieves the exact same number. I haven't been able to figure out why. E.g. The first loop returns the value of 6, but so does the second loop. I am trying to retrieve 6 if it exists from the first loop and 7 if it exists in the second loop. 7 is located in the array, so it should return 6 in the first for statement and 7 on the second.
Here is what I have done so far:
for ($i = 0; $i < count($final_data); $i++) {
$check_sector = $final_data[$i]['sector'];
$check_image = $final_data[$i]['image'];
if($final_data[$i]['sector'] == 6){
echo "<div id='6' class='w3-button w3-ripple grid-item-sector' onclick='getSector(this.id)'>"
. "<div id='ss6' style='position: absolute; width: 100px; height: 100px; background-image: url(" . $check_image . ");'></div>"
."<div id='s6' class='overlay' ></div></div>";
break;
}else{
echo "<div id='6' class='w3-button w3-ripple grid-item-sector' onclick='getSector(this.id)'>"
."<div id='s6' class='overlay' ></div></div>";
break;
}
}
for ($i = 0; $i < count($final_data); $i++) {
$check_sector = $final_data[$i]['sector'];
$check_image = $final_data[$i]['image'];
if($final_data[$i]['sector'] == 7){
echo "<div id='7' class='w3-button w3-ripple grid-item-sector' onclick='getSector(this.id)'>"
. "<div id='ss7' style='position: absolute; width: 100px; height: 100px; background-image: url(" . $check_image . ");'></div>"
."<div id='s7' class='overlay' ></div></div>";
break;
}else{
echo "<div id='7' class='w3-button w3-ripple grid-item-sector' onclick='getSector(this.id)'>"
."<div id='s7' class='overlay' ></div></div>";
break;
}
}
When you get to the second foreach loop, it’s hitting the first element in the array first.
So it’s saying is 6==7? No, so it goes to the else echo statement.
Then you have a break so it gives up and never checks the second element in the array which is the 7.
I need to add a line break after each row retrieved;
I hav tried adding in every possible way.. No luck..
while ($line = mysql_fetch_array($result))
{
?>
<style>
.whiteBackground { background-color:#F5ECCE;float:left;}
.grayBackground { background-color:#CED8F6; float:right;}
.date { font-size:10px; color:#627d79;float:right}
</style>
<?php
$sender=$line["sender"];
$classy=($sender == $uid)? 'whiteBackground': 'grayBackground';
$q=mysql_query("select * from users where u_id='$sender'");
$row=mysql_fetch_array($q);
$receiver=$row['firstname'];
//if($receiver!= $line["receiver"])
$msg = $msg . "<tr class='$classy'>" .
"<td>" . $receiver . ": </td>" .
"<td>" . $line["msg"] . "</td>"."<td class='date'>" . $line["chattime"] . "</td></tr>";
}
$msg=$msg . "</table>";
echo $msg;
I need to add a break after each sender+msg+time.//like in a usual chat.
Your problem come from float declarations. They are useless in your code :
.whiteBackground { background-color:#F5ECCE;float:left;}
.grayBackground { background-color:#CED8F6; float:right;}
.date { font-size:10px; color:#627d79;float:right}
Remove them and it will work as expected.
You don't need them in your code if you use <table> and <tr>. Default behaviour of <tr> is to create a new "line" each time you add it, but with adding float you're breaking this behaviour.
Also, follow #bulforce recommendation to move your CSS before loop : your code add it as many time you have messages in your document (if you have 10 messages, css declarations will be added 10 times).
<style type="text/css">
.whiteBackground { background-color:#F5ECCE; }
.grayBackground { background-color:#CED8F6; }
.date { font-size:10px; color:#627d79; }
</style>
<?php
while ($line = mysql_fetch_array($result))
{
// ...
}
?>
Just try to add a <tr><td colspan="3"> </td></tr> after </tr> inside the while loop
First consider moving the css declaration before the loop, there is no point to have it in the loop body.
Second the second query in the loop should also be moved before the loop and rework this a bit.
Now to your question, one option would be to change the table with divs and spans.. something like so:
<div class="message-row">
<span class="message-user">Name</span>
<span class="message-text">Message</span>
</div>
Then style as you did before, and add margin-bottom: 15px; to the message-row definition.
before while
$msg="<table>";
Inside while
$msg.= "<tr class='$classy'>" in place of $msg = $msg . "<tr class='$classy'>"
Will work for you.
I solved it by replacing table tags with div tags.
Thnku #bulforce; now I will completely follow ur recommendations.
$msg="<div>";
$date_temp="nil";
while ($line = mysql_fetch_array($result))
{
?>
<style>
.whiteBackground { background-color:#F5ECCE;float:left;}
.grayBackground { background-color:#CED8F6; float:right;}
.date { font-size:10px; color:#627d79;float:right}
</style>
<?php
$sender=$line["sender"];
$classy=($sender == $uid)? 'whiteBackground': 'grayBackground';
$q=mysql_query("select * from users where u_id='$sender'");
$row=mysql_fetch_array($q);
$receiver=$row['firstname'];
//if($receiver!= $line["receiver"])
/*if($date_temp!=$line["chatdate"]){
$date_temp=$line["chatdate"];
$msg=$msg.$line["chatdate"];
}*/
$msg = $msg . "<div class='$classy'>" . $receiver.": " . $line["msg"] ."<div class='date'>" . $line["chattime"] . "</div></div></br></br>";
}
$msg=$msg . "</div>";
echo $msg;
I've been working on this list that will provide information from an xml file. The markup I'm working with is as follows:
<?php
$xml_file = '/path';
$xml = simplexml_load_file($xml_file);
foreach($xml->Continent as $continent) {
echo "<div class='continent'>".$continent['Name']."<span class='status'>".$continent['Status']."</span>";
foreach($continent->Country as $country) {
echo "<div class='country'>".$country['Name']."<span class='status'>".$country['Status']."</span>";
foreach($country->City as $city) {
echo "<div class='city'>".$city['Name']."<span class='status'>".$city['Status']."</span></div>";
}
echo "</div>"; // close Country div
}
echo "</div>"; // close Continent div
}
?>
What I'm trying to accomplish is changing the 'Status'. Regardless of the element, the 'Status' only has two values and will always be either "Red" or "Blue". What I'm trying to do is go through the whole document, find the "Status" attribute, see if the value is "Red", then display an icon or some text, but if it's blue, display nothing. I've come across different examples, but none that show this type of generality. Most of the examples were for locating a specific node and then displaying it's attributes. I'm also trying to avoid, if possible, having to create this rule within each loop, but instead applying it to the whole document (if this seems like the best method.)
Use css selectors instead of javascript. That will make your code lighter and simple!
For example:
<?php
$xml_file = '/path';
$xml = simplexml_load_file($xml_file);
foreach($xml->Continent as $continent) {
echo "<div class='continent'>".$continent['Name']."<span class='status ". cssForStatus($continent['Status'])."'></span>";
foreach($continent->Country as $country) {
echo "<div class='country'>".$country['Name']."<span class='status ". cssForStatus($country['Status'])."'></span>";
foreach($country->City as $city) {
echo "<div class='city'>".$city['Name']."<span class='status ". cssForStatus($city['Status'])."'></span></div>";
}
echo "</div>"; // close Country div
}
echo "</div>"; // close Continent div
}
function cssForStatus($status='')
{
switch ($status) {
case 'red':
$css = "red";
break;
default:
$css = "blue";
break;
}
return $css;
}
while your css can be:
span.status {
height: 10px;
width: 10px;
}
span.status.red {
background-color: "red";
}
span.status.blue {
background-color: "blue";
}
we are using the concept of multiple css classes
Just change your code slightly to add status as your div class.
Example:
echo "<div class='continent status-{$continent['Status']}'>{$continent['Name']}</div>";
This will output (in case of a red continent named Europe)
<div class='continent status-red'>Europe</div>
then add this CSS to your html:
.status-red {
background-color: red;
}
.status-blue {
background-color: blue;
}
Or somehting along these lines
Ok i have this code so far which is in my wordpress template so precisely this a wordpress stuff.
<?php
$post_id = 266;
echo "<div id='widgets-wrapper3'><div id='marginwidgets' style='overflow: auto; max-width: 100%; margin: 0 auto; border: none !important;'>";
$queried_post = get_post($post_id);
echo "<div class='thewidgets'>";
echo $queried_post->post_content;
echo '</div>';
echo "</div></div>";
?>
as you can see into the code, the routine is, to display the post which has an id of 266, now all i want is to limit the word count in the post content of that post, let say I want to limit the word to 300 and then add a read more link. how to make that please?
hope there's someone here who figured out how to make that.
I am open in, Ideas, recommendation and suggestion. Hope someone here could help, thank you.
try this:
http://codex.wordpress.org/Function_Reference/the_excerpt
or use php substr:
echo get_sub($queried_post->post_content, 300);
function get_sub($str, $max=300)
{
$ar = explode($str);
$count = 0;
$new_str = "";
$del = " ";
foreach($ar as $a)
{
if($count == 0)
{
//no space
$del = "";
}
if($count < $max)
{
$new_str .= $del.$a;
}
$count++;
}
return $new_str;
}
if the content contains html elements, its a problem.
hope it helps
I have this code.
<html>
<head>
<style type="text/css">
body{background:#666666;}
div{border:1px solid red;}
</style>
</head>
<body>
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("juliver", $con);
$result = mysql_query("SELECT * FROM hi");
while($row = mysql_fetch_array($result))
{
echo '<img src="'.$row['name'].'" />';
echo "<div>".$row['name']."</div>";
echo "<div>".$row['title']."</div>";
echo "<div>".$row['description']."</div>";
echo "<div>".$row['link']."</div>";
echo "<br />";
}
mysql_close($con);
?>
</body>
</html>
the above code works.
now, I want to insert this
echo '<img src="'.$row['name'].'" />';
echo "<div>".$row['name']."</div>";
echo "<div>".$row['title']."</div>";
echo "<div>".$row['description']."</div>";
echo "<div>".$row['link']."</div>";
echo "<br />";
into the specified div or other element in the html, example, i will insert this
echo '<img src="'.$row['name'].'" />';
into the html element .
I dont know how to do this, please help me. Thanks
Juliver
if yo want to place in an div like
i have same work and i do it like
<div id="content>
<?php
while($row = mysql_fetch_array($result))
{
echo '<img src="'.$row['name'].'" />';
echo "<div>".$row['name']."</div>";
echo "<div>".$row['title']."</div>";
echo "<div>".$row['description']."</div>";
echo "<div>".$row['link']."</div>";
echo "<br />";
}
?>
</div>
The only things I can think of are
including files
replacing elements within files using preg_match_all
using assigned variables
I have recently been using str_replace and setting text in the HTML portion like so
{{TEXT_TO_REPLACE}}
using file_get_contents() you can grab html data and then organise it how you like.
here is a demo
myReplacementCodeFunction(){
$text = '<img src="'.$row['name'].'" />';
$text .= "<div>".$row['name']."</div>";
$text .= "<div>".$row['title']."</div>";
$text .= "<div>".$row['description']."</div>";
$text .= "<div>".$row['link']."</div>";
$text .= "<br />";
return $text;
}
$htmlContents = file_get_contents("myhtmlfile.html");
$htmlContents = str_replace("{{TEXT_TO_REPLACE}}", myReplacementCodeFunction(), $htmlContents);
echo $htmlContents;
and now a demo html file:
<html>
<head>
<style type="text/css">
body{background:#666666;}
div{border:1px solid red;}
</style>
</head>
<body>
{{TEXT_TO_REPLACE}}
</body>
</html>
You can write the php code in another file and include it in the proper place where you want it.
AJAX is also used to display HTML content that is formed by PHP into a specified HTML tag.
Using jQuery:
$.ajax({url: "test.php"}).done(function( html ) {
$("#results").append(html);
});
Above code will execute test.php and result will be displayed in the element with id results.
There is no way that you can do it in PHP when HTML is already generated. What you can do is to use JavaScript or jQuery:
document.getElementById('//ID//').innerHTML="HTML CODE";
If you have to do it when your URI changes you can get the URI and then split it and then insert the HTML in script dynamically:
var url = document.URL;
// to get url and then use split() to check the parameter
refer to the basic.
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john#example.com')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
there is no way to specifically target an element with php, you can either embed the php code between a div tag or use jquery which would be longer.
You can repeat it by fetching again the data
while($row = mysql_fetch_assoc($result)){
//another html element
<div>$row['name']</div>
<div>$row['title']</div>
//and so on
}
or you need to put it on the variable and call display it again on other html element
$name = $row['name'];
$title = $row['title']
//and so on
then put it on the other element, but if you want to call all the data of each id, you need to do the first code
Have you tried this?:
$string = '';
while($row = mysql_fetch_array($result))
{
//this will combine all the results into one string
$string .= '<img src="'.$row['name'].'" />
<div>'.$row['name'].'</div>
<div>'.$row['title'].'</div>
<div>'.$row['description'].'</div>
<div>'.$row['link'].'</div><br />';
//or this will add the individual result in an array
/*
$yourHtml[] = $row;
*/
}
then you echo the $tring to the place you want it to be
<div id="place_here">
<?php echo $string; ?>
<?php
//or
/*
echo '<img src="'.$yourHtml[0]['name'].'" />;//change the index, or you just foreach loop it
*/
?>
</div>
Well from your code its clear that $row['name'] is the location of the image on the file, try including the div tag like this
echo '<div>' .$row['name']. '</div>' ;
and do the same for others, let me know if it works because you said that one snippet of your code is giving the desired result so try this and if the div has some class specifier then do this
echo '<div class="whatever_it_is">' . $row['name'] . '</div'> ;
You have to put div with single quotation around it. ' ' the Div must be in the middle of single quotation . i'm gonna clear it with one example :
<?php
echo '<div id="output">'."Identify".'<br>'.'<br>';
echo 'Welcome '."$name";
echo "<br>";
echo 'Web Mail: '."$email";
echo "<br>";
echo 'Department of '."$dep";
echo "<br>";
echo "$maj";
'</div>'
?>
hope be useful.
I use this or similar code to inject PHP messages into a fixed DIV positioned in front of other elements (z-index: 9999) just for convenience at the development stage.
Each PHP message passes into my 'custom_message()' function and is further conveyed into the innard of preformatted DIV created by echoed JS.
There can be as many as it gets, all put inside that fixed DIV, one under the other.
<style>
#php_messages {
position: fixed;
left: 0;
top: 0;
z-index: 9999;
}
.php_message {
background-color: #333;
border: red solid 1px;
color: white;
font-family: "Courier New", Courier, monospace;
margin: 1em;
padding: 1em;
}
</style>
<div id="php_messages"></div>
<?php
function custom_message($output) {
echo
'
<script>
var
el = document.createElement("DIV");
el.classList.add("php_message");
el.innerHTML = \''.$output.'\';
document.getElementById("php_messages").appendChild(el);
</script>
';
}
?>