Changing the presentation of a query - php

I have this code to output records from a database
<?php
include 'database_conn.php'; // make db connection
$sql = "SELECT filmID, title, categoryName, notes FROM film";
$queryresult = mysqli_query($conn, $sql) or die(mysqli_error($conn));
while ($row = mysqli_fetch_assoc($queryresult)) {
$filmID = $row['filmID'];
$filmTitle = $row['title'];
$filmCat = $row['categoryName'];
$filmNotes = $row['notes'];
echo "<div>$filmID, $filmTitle, $filmCat, $filmNotes</div>";
}
mysqli_free_result($queryresult);
mysqli_close($conn);
?>
but to be able to remove commas and format the display of the content I need to change...
echo "<div>$filmID, $filmTitle, $filmCat, $filmNotes</div>";
to...
echo
"<div class="film">
<span class="filmID">5</span>
<span class="title">my darling clementine</span>
<span class="category">epic</span>
<span class="notes">Henry Fonda</span>
</div>";
I keep getting an error when submitting the form saying (Parse error: syntax error, unexpected '<' in /home/unn_w15025267/public_html/chollerton.php on line 45)
Line 45 is:
"<div class="film">
Records currently look like this:
Patrick, Bloggs, 15, Blogs Street, Durham, DH2 ABC, 0191 123 4567, 07001212111, patrick&sheila#madeupemail.com, SMS
Quite new to this so any help would be amazing

The correct code would be the following:
echo
"<div class='film'>
<span class='filmID'>5</span>
<span class='title'>my darling clementine</span>
<span class='category'>epic</span>
<span class='notes'>Henry Fonda</span>
</div>";
As the first double quote starts the string and the next closes the string.
SO "<div class="film"> is erroneous.
Or you may as well escape the double quote on the inside:
echo
"<div class=\"film\">
<span class=\"filmID\">5</span>
<span class=\"title\">my darling clementine</span>
<span class=\"category\">epic</span>
<span class=\"notes\">Henry Fonda</span>
</div>";

You are using double quotation inside string literal. It will confuse the parser. Use single quotation instead:
echo
"<div class='film'>
<span class='filmID'>5</span>
<span class='title'>my darling clementine</span>
<span class='category'>epic</span>
<span class='notes'>Henry Fonda</span>
</div>";

Try a heredoc
echo <<<HERE
<div class="film">
<span class="filmID">$filmID</span>
<span class="title">$filmTitle/span>
<span class="category">$filmCat</span>
<span class="notes">$filmNotes</span>
</div>
HERE;

Assuming that you want to use the variables you got from the DB, I think this is what you want. This answer also assume there is no PHP above this HTML. If there is you would have to leave PHP parsing (?>) before these lines.
<div class="film">
<span class="filmID"><?php echo $filmID; ?></span>
<span class="title"><?php echo $filmTitle; ?> </span>
<span class="category"><?php echo $filmCat; ?></span>
<span class="notes"><?php echo $filmNotes; ?></span>
</div>
<?php
//resume PHP parsing (if needed)

Related

MYSQL error 500

Issue
I'm currently attempting to create a "cell" for each mysql row similar to how twitter has it, but for some reason i'm logging
PHP Parse error: parse error, expecting `','' or `';''
Code
I'm fairly new to PHP I am unsure of why my code is creating this error.
<?php
while ($row = mysql_fetch_array($rs)) {
$unformatted = $row['mcap_HKD'];
$output = number_format($unformatted);
$time = time_elapsed_string($row['time']);
echo
"<div class="content">
<div class="headers">
<div class="info">
<strong class="scode">".$row['scode']."</strong><br>
<span class="sname">".$row['sname']."</span><br>
<span class="industry">".$row['main_industry']."</span>
</div>
<small class="time">."$time."</small>
</div>
<div class="news">
".$row['title']."
</div>
</div>";
}
I know that through testing that I am able to reach the server and that it will output the data just fine on its own, and even when i output single variables onto one div, but for some reason this is not working when i put extra divs like i do above. I have attempted to echo each line by itself but it still returns the same error.
Issue
PHP use " as a string delimiter (it has several string delimiters). When, in you echo statement, you are not escaping the ", it's as if you stop the string. So PHP is waiting for a string concatenation . or , or the ; end of statement character.
Resolution
You must escape " characters in you echo statement as such: \" ; it should look like:
Escape string delimiters
echo
"<div class=\"content\">
<div class=\"headers\">
<div class=\"info\">
<strong class=\"scode\">".$row['scode']."</strong><br>
<span class=\"sname\">".$row['sname']."</span><br>
<span class=\"industry\">".$row['main_industry']."</span>
</div>
<small class=\"time\">."$time."</small>
</div>
<div class=\"news\">
".$row['title']."
</div>
</div>";
Mix HTML and PHP
or do something like:
<?php while ($row = mysql_fetch_array($rs)): ?>
<?php
$unformatted = $row['mcap_HKD'];
$output = number_format($unformatted);
$time = time_elapsed_string($row['time']);
?>
<div class="content">
<div class="headers">
<div class="info">
<strong class="scode"><?= $row['scode']; ?></strong><br>
<span class="sname"><?= $row['sname']; ?></span><br>
<span class="industry"><?= $row['main_industry']; ?></span>
</div>
<small class="time"><?= $time; ?></small>
</div>
<div class="news">
<?= $row['title']; ?>
</div>
</div>
<?php endwhile; ?>
This has the benefit to not use PHP to print HTML tags, and with the <?= PHP opening tags, it's more readable!
I hope this helps you! :)

Browser is putting in a p tag and ignoring my span

This is my code in the php file. excontent is an array, $rebuild is a string.
<div class="about-hero">
<h2><span class="accel"><?php echo $excontent[0] . " "; ?></span><?php echo $rebuild; ?> </h2>
</div>
This is what I get when I inspect the element:
<div class="about-hero">
<h2>
<span class="accel">
<p>
"Accelerate is a marketing agency located in NYC. A great blah blah blah."
</p>
</span>
</h2>
</div>
Any ideas why its putting in a p tag??? Its something with the PHP and I'm new to PHP. I tried it with just strings and it works fine. I'm changing the color of the first word in a sentence (following the design) the rebuild is the rest of the sentence. They both echo fine on their own and show the correct content.
I'm assuming the <p> tag is on the $excontent[0] variable. In this case, you could try removing all HTML tags from it:
<div class="about-hero">
<h2><span class="accel"><?php echo strip_tags($excontent[0]) . " "; ?></span><?php echo $rebuild; ?> </h2>
</div>
Or removing just the trailing and leading <p> and </p>:
<div class="about-hero">
<h2><span class="accel"><?php echo substr(trim(substr(trim($excontent[0]), 3, strlen($excontent[0]))), 0, -4) . " "; ?></span><?php echo $rebuild; ?> </h2>
</div>
Your Browser will not "put" something into your HTML without any reason.
The first result of the Array is the place you should look at. You didn't provided your PHP Code, but im sure that result is where you find the <p> Tag ...
Just try this in JavaScript:
alert(<?PHP echo $excontent[0]; ?>);

Unable to Display Images from MySQL

I am unable to display images from MySQL. I've checked path etc. but still there are broken images on site. Below is my Code. Please guide me.
<div id="right_content">
<div id="headline">
<div id="headline_content"> <b>Welcome Guest!</b> <b style="color:#F56013">Shopping Cart</b> <span>-Items: -Price: </span> </div>
</div>
<div id="products_box">
<?php
$get_products ="SELECT * FROM products ORDER BY rand() LIMIT 0,6";
$run_products = mysqli_query($con,$get_products);
while($row_products=mysqli_fetch_array($run_products)){
$prod_id = $row_products['product_ID'];
$prod_title = $row_products['product_title'];
$prod_cat = $row_products['category_ID'];
$prod_brand = $row_products['brand_ID'];
$prod_price = $row_products['product_price'];
$prod_desc = $row_products['product_desc'];
$prod_img =$row_products['product_img1'];
echo "
<div id='single_product'>
<h3>$prod_title</h3>
<img src='admin_area/product_images/$prod_img' width='180' height='180' />
</div>
";
}
?>
</div>
As per comment, problem is solved. The images were not moving into the directory and i didn't check that. When i manually copied them into the specified directory the problem was gone.
When you write html please note that you should always use double and not single quotes for tag properties. So for example
src='admin_area/product_images/$prod_img'
should be
src="admin_area/product_images/$prod_img"
and so on. Since you are doing echo using double quotes you should escape double quotes with .
So your echo should look like:
echo "
<div id=\"single_product\">
<h3>$prod_title</h3>
<img src=\"admin_area/product_images/$prod_img\" width=\"180\" height=\"180\" />
</div>
";
IMHO it is better to write plain html and use php only to echo variables just to avoid this kind of problems with the generated code. So I'd write:
?>
<div id="single_product">
<h3><?php echo $prod_title; ?></h3>
<img src="admin_area/product_images/<?php echo $prod_img; ?>" width=\"180\" height=\"180\" />
</div>
<?php } ?>
This will save you a lot of headache in case of debugging if your code is not working

Remove the closing </ span> from the expression contains more than one

There is a very large piece of code that does not work out well when specific syntax html.
There is an expression:
<span class="*0">
<span class="*1">TEXT</span>
...
<span class="*2">TEXT</span>
</span>
There is a regular expression:
$mstr = '#<span class="0">(.*?)</span>#';
What is needed:
Cut the upper span (<span class = "* 0">) with the correct closing tag.
My regular cuts out the first in a row :(
Here is a solution. I don't know if it fits your needs, but it does the job. It simply looks for all the starting tags and closing tags, stores their substring positions and pairs them. Then it removes the tag with the class you need.
One note: if a tag is not propperly closed, this could fail. So I would suggest you build in some safety measures.
$start_pos=stripos($var,'<span class="*0">');
$len=strlen($var);
$str_len=strlen('<span class="*0">');
$offset=0;
do{
$p=stripos($var,'<span',$offset);
if($p===false){break;}
$open_pos[]=$p;
$offset=$p+1;
}while($offset<$len);
$offset=0;
do{
$p=stripos($var,'</span>',$offset);
if($p===false){break;}
$close_pos[]=$p;
$offset=$p+1;
}while($offset<$len);
$t=0;
do{
$change=false;
for($i=0;$i<count($open_pos)-1;$i++){
foreach($close_pos as $k=>$v){
if($open_pos[$i+1]>$v){
if($open_pos[$i]==$start_pos){
$end_pos=$v;
break 3;
}
unset($open_pos[$i],$close_pos[$k]);
$open_pos=array_values($open_pos);
$close_pos=array_values($close_pos);
$change=true;
break 2;
}
}
}
if($open_pos[$i]!=$start_pos){
unset($open_pos[$i],$close_pos[0]);
$open_pos=array_values($open_pos);
$close_pos=array_values($close_pos);
$change=true;
}
else{
$end_pos=$close_pos[0];
break 3;
}
if(count($open_pos)<2)break;
$t++;
}while($t<1000);
$var=substr_replace($var,'###',$end_pos,7);
$var=substr_replace($var,'###',$start_pos,$str_len);
echo $var;
Tested on this beautiful HTML:
$var='<span class="*A">a
<span class="*B">b
<span class="*E">e</span>
<span class="*C">c
<span class="*D">d
<span class="*E">e</span>
<span class="*0">BEFORE THIS ONE
<span class="*F">a</span>
<span class="*G">g
<span class="*H">h
<span class="*J">j</span>
</span>
<span class="*K">k</span>
<span class="*L">l</span>
<span class="*M">m</span>
_GGG</span>
<span class="*N">n</span>
BETWEEN</span>BETWEEN
<span class="*O">o
<span class="*P">p</span>
_OOO</span>
</span>
_CCC</span>
<span class="*Q">q
<span class="*R">r</span>
_RRR</span>
</span>
</span>
';

php preg_replace the last link of two

I have many links out of one foreach. each foreach output some dom tree like:
<span id="span1">
<a(.*?)/test/(.*?)>word1</a>
</span>
<span id="span2">
<a(.*?)/fold/(.*?)>word2</a>
</span>
Now I want to replace the last link of the two, change the whole code as:
<span id="span1">
<a(.*?)/test/(.*?)>word1</a><!-- remain this link, do not replace. -->
</span>
<span id="span2">
word2
</span>
My preg_replace code here:
$code = '<span>test1</span><span>test2</span>';
echo preg_replace('%href="(.*?)/fold/(.*?)"%', 'href="#" class="replaced" title="$2"', $code);
I want get code like
<span id="span1">
test1
</span>
<span id="span2">
test2
</span>
But it will output <span id="span1">word2</span>, not as I expected. how to do well? thanks.
this will work (fixed):
preg_replace('(href="(.*?)/fold/(.*?)">(.*?)</a>)', 'href="#" class="replaced" title="$3">$3</a>', $code);
Thanks for onatm suggestion, finnally, I use simple_html_dom make a judge and get the code what I need.
$code = <<<EOT
<span id="span1">word1</span><span id="span2">word2</span>
EOT;
$html = str_get_html($code);
if($html->find("span[id=span1]")) {
foreach($html->find("span[id=span1]") as $data1)
$result1 = $data1;
}
if($html->find("span[id=span2]")) {
foreach($html->find("span[id=span2]") as $data2)
$result2 = preg_replace('%href="(.*?)/fold/(.*?)">(.*?)</a>%', 'href="#" class="replaced" title="$3">$3</a>', $data2);
}
echo $result1.''.$result2;

Categories