PHP echo resulting in jumbled up string - php

Basically I want to create a div with specific styling using PHP.
I have the following intended style saved up in a separate string for easy editing:
$bg = "background: url('./flags/" . $country[$id[0]]["iso"] . ".png')no-repeat center center fixed;";
And this is the echo that generates the div:
echo "<div class='flag' style='" . $bg . "'></div>";
When I run this code, the div does appear, but the style part is all jumbled up and weirdly formatted, like so:
<div class="flag" style="background:url(\" .="" flags="" hk.png\')no-repeat="" center="" fixed;'=""></div>
What is causing this problem?
Thanks in advance.

I guess I'll repost this here, since it worked out:
In the $bg variable, you're encasing the url in single quotes -- but in your echo statement, you're doing the same thing for the style attribute. So when your $bg variable renders, it's closing the single quotes. I could be wrong, but I would try switching out the quotes in your $bg variable like this:
$bg = 'background: url("./flags ... etc ...

Related

How to wrap output of echo in a span

Excuse the basic question but I'm new to PHP! How can I wrap the below text in span to change the font size of all the output fields. I managed the Name field but due to the periods I can't get the others working. Hopefully I can just wrap the entire echo in the same span.
echo "<span style=\"font-size: 10px;\">$row->Name</span>"." ".$row->Country." ".$row->Location." ".$row->Category." ".$row->Rating." ".$row->Review." ".$row->StartDate." ".$row->EndDate." ".$row->URL."<br>";
Try some thing like this:
echo '<span style="color:red;">'.$variable.'</span>';

How do I change class name for CSS inside a PHP Loop?

Hello I am trying to change CSS content on some DIVs depending of their class name.
To explain better, I have a while loop in PHP reading from the database to output DIVs and I have a field named "section" with data such as A,B,C,D,E,F,G.
For the DIVs located in section "A" and "B" I want the class name to be desk_box_hor (for horizontal) ELSE I want it to desk_box_ver(vertical).
Below is what I tried doing only two sections (A,B) that need to be vertical. The others need to be horizontal. If theres a more efficient way of doing this please tell me. I have about 200 of these DIVs being output on screen.
If you have a better title please recommend one, I didn't know what to put lol.
Thanks in advance!
My fiddle of what I want both DIVs to look like
PHP:
while($row = mysqli_fetch_assoc($desk_coord_result)){
//naming X,Y values
$id = $row['coordinate_id'];
$x_pos = $row['x_coord'];
$y_pos = $row['y_coord'];
$sec_name = $row['section_name'];
//draw a DIV box at its X,Y coordinate
//if section A and B do vertical
if($sec_name == 'B' || $sec_name == 'A'){
echo '<div class="desk_box_ver" data="'.$id.'" style="position:absolute;left:'.$x_pos.'px;top:'.$y_pos.'px;">id:'.$id.'</div>';
}
//else do it horizontal
else{
echo '<div class="desk_box_hor" data="'.$id.'" style="position:absolute;left:'.$x_pos.'px;top:'.$y_pos.'px;">id:'.$id.'</div>';
}
} //end while loop for desk_coord_result
CSS:
/* desk boxes*/
.desk_box_ver{
width: 23px;
height: 10px;
border: 4px solid black;
padding:10px;
}
.desk_box_hor{
width: 10px;
height: 23px;
border: 4px solid black;
padding:10px;
}
Also, lets say I want to use these two classes in the same Jquery function, is this the proper way of doing it?
$(".desk_box").add(".desk_box_ver").click(function() {
or
$(".desk_box, .desk_box_ver").click(function() {
In answer to your question "If theres a more efficient way of doing this please tell me." (I'll leave your other questions to someone else) yes, there are more efficient ways to write this PHP code, which then makes debugging and maintenance easier:-
a) Instead of two very long echo strings which are almost exactly the same, introduce a new PHP variable, say, $class. Then write:
$class = 'desk_box_hor';
if($sec_name == 'A' || $sec_name == 'B'){
$class = 'desk_box_ver';
}
echo '<div class="' . $class . '" data="' . $id . '" style="position:absolute;left:' . $x_pos . 'px;top:' . $y_pos.'px;">id:' . $id . '</div>';
Now you only have one long echo string to write, and to debug.
Also my preference is (though this is opinion only) to put a space either side of all those string concatenation dot operators - it makes it easier to decipher whats going on.
b) The next improvement you can make is to swap all the single and double quotes. Then you can write a single string with no concatenation operators at all, as you can put a PHP variable inside double quotes. Again, it makes the string of html clearer and easier to read, and debug. (Browsers can handle single or double quotes in the HTML tags). You end up with:
echo "<div class='$class' data='$id' style='position:absolute;left: $x_pos" . "px;top: $y_pos" . "px;'>id:$id</div>";
c) Next we can make the HTML code being created more readable; at the moment your script is generating a huge block of HTML markup (200 divs?) with no line breaks. Horrendous to debug. Just add \n to the end of your echo string like so:
echo ".....id:$id</div>\n";
and that will split the generated HTML markup into separate lines (but the onscreen text will be unaffected). Much easier then to see if one of the items went wrong (for instance, if the database returns an empty value for one of the records, it will stand out in the HTML like a sore thumb). Note that you can add \n inside a string surrounded by double quotes; if you stay with the original single quoted string you would have to add with a dot operator:
.....id:$id</div>' . "\n";
d) Lastly, you could cut out all those 200 position:absolute strings from the generated HTML markup by putting it into your CSS stylesheet, just retaining the CSS values that vary. Ie:
.desk_box_hor, .desk_box_ver { position: absolute; }
As regards why you are only getting vertical divs, never horizontal, I'll just try an educated guess. Are you really getting back from the database what you think you are?
For instance, you say in your comments the field name is "section", but the PHP is looking for a "section_name" field. Or is the data itself wrong? Have you got PHP error checking on, eg error(reporting(E_ALL)? If not, it would not return an error message, but still blindly go on reading through all rows in the db.
In that case it will always take the else part of your if...else. Supposedly this is the horizontal div path, but because the CSS has the width and height values the wrong way round (see above) it will actually produce vertical boxes all the time.

php - get the source of an image

I need to get all the source values from all image inside a container. I'm having some difficulty with this.
Allow me to explain the process.
All the data comes from a database. Inside the backofficce the user enter all the text and the image inside a textarea. To separate the text with the image the user must enter a pagebreak.
Let's go to the code
while ($rowClients = mysql_fetch_array($rsClients)) {
$result = $rowClients['content'];
$resultExplode = explode('<!-- pagebreak -->', $result);
// with resultExplode[0] I get the code and with resultExplde[1] I get the image
// Now with I want to get only the src value from resultExplode[1]
I already tried with strip_tags
$imageSrc = strip_tags($resultadoExplode[1]);
but it doesn't print anything.
I found this post but without success. I stopped in the first print_r.
Can anyone help me??
Thanks
try foreach, if you can't print it out.. (if that's the problem)
foreach($resultExplode as $key => $value){
echo "[".$key."]".$value;
}
I found a solution:
continuing with the previous code I worked with the split function.
So I start to strip the tags. This way I get the img isolated from the rest.
$image = strip_tags($resultExplode[1],"<img>");
Because all the img has the same structure like this: <img width="111" height="28" alt="alternative text" src="/path/to/the/file.png" title="title of the image">
I split this string, using " as a delimiter
$imgSplit = split('"', $image);
$src = $imgSplit[3];
Voilá. It's working
What do you say about this procedeure??

include a PHP result in img src tag [duplicate]

This question already has answers here:
php - insert a variable in an echo string
(10 answers)
Closed 4 years ago.
I am trying to do something I know is probably simple, but I am having the worst time.
I have functioning so far:
1.Script to upload image files to server
2. write the image file names to the database
3. I want to retrieve the image filename from the db and add it to the img src tag
here is my retrieval script
<?php
$hote = 'localhost';
$base = 'dbasename';
$user = 'username';
$pass = '******';
$cnx = mysql_connect ($hote, $user, $pass) or die(mysql_error ());
$ret = mysql_select_db ($base) or die (mysql_error ());
$image_id = mysql_real_escape_string($_GET['ID']);
$sql = "SELECT image FROM image_upload WHERE ID ='$image_id'";
$result = mysql_query($sql);
$image = mysql_result($result, 0);
header('Content-Type: text/html');
echo '<img src="' $image'"/>';
?>
I was trying to pass the Value through image.php?ID=2 but no luck
The PHP script successfully returns the filename, but I cannot for the life of me get it to print it to the html
Any suggestions, please and thank you very much :)
OK, it does return the proper tag, but now it seems as though the script doesnt run to generate the tag.
I have tried two ways:
<div class="slides">
<div class="slide">
<div class="image-holder">
<?php
include ("image.php?ID=2");
?>
</div>
and:
<img src="image.php?ID=2" alt="" />
but neither one will insert the filename...
I need to identify each img src by the primary key, so I was passing it the ID from each image src location
but alas, my PHP ninja skills need to be honed.
Just to clarify: I am uploading images to the server, recording the filenames in a DB and calling that filename in an HTML doc...there are several in each one so I need to pass the ID (i.e. 1,2,3 ) to correspond to the primary key in the table.
But I cant get the script to process the tag first.
If I go to view source, I can click the script and get the proper result...
Thanks again, you guys and girls are very helpful
You're missing the concatenation operator: .:
echo '<img src="' . $image . '"/>';
You can do it as you did but you had the single quotes in twice, (unless you were meaning to use concatenation - which is unnecessary - if you want this see the other answer).
echo "<img src=\"$image\"/>";
Or the longer form with braces if you need to embed inside text.
echo "<img src=\"${image}\"/>";
I'd recommend using heredoc syntax for this if you're doing lots of HTML. This avoids the need to have lots of echo lines.
echo <<< EOF
<div class="example">
<img src="$image" />
</div>
EOF;
Try using this syntax:
echo "<img src=\"$imagePath\" />";
It works with double quotes provided you escape the quotes in the src attribute. Still not sure why the singles don't work.
there are 2 major flaws with your design
There is no image.php?ID=2 file on your disk.
There is absolutely no point in including image.php file. You have to get the name right in the file you are working with. don't you have this row already selected from the database? Why not just print the image name then?
And yes, you are using single quotes where double ones needed.

Why doesn't PHP appear to store my variables?

I have a page that allows the user to draw an image using HTML5 canvas, convert it into text with JavaScript and post it to a PHP page.
http://dsiextensions.co.cc/chatdraw.php
The page is quite confusing, each text box is for each line of to 100px X 100px canvas. To put the data in the boxes, click "Finish" and then click "Submit" (Sorry that it's really slow).
I've tries making changes to the PHP code and occasionally the variable turns up but more often than not, it doesn't.
Here's the code: (note, only the data in the first box is used at the moment)
<?php
$dstring = $_POST['senddata1'];
$darray = str_split($dstring);
echo $dstring;
print_r($darray);
$x=1;
$y=1;
for ($a=0;$a<100;$a++)
{
if($a%100==0 && $a!=0){
echo '<br />'; //Checks if it is the 100th pixel and adds a new line (not needed at the moment)
$y++;
$x=1;}
//echo $x . ',' . $y . '(' . $a . ',' . $darray[$a] . ')|';
if($darray[$a]!=0){
echo "<input type='button' style='width:15;height:15;background-color:#000' />"; //Black button if it is a black pixel
}
else{
echo "<input type='button' style='width:15;height:15;background-color:#fff' />"; //White button if it is a white pixel
}
$x++;
}
?>
The codes supposed to check if the pixel is black or white and create a coloured button based on that (I'm going to use the image functions later), however $dstring is never echoed and therefore can't be converted to an array. Is there something I'm doing wrong here or is it a server problem?
Thanks
Note that the second PHP line should be spread out on to three lines
Seems to work by my side. Are you sure that data is well posted ? Try to make a print_r($_POST); in your script to see what has been posted

Categories