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! :)
Related
This error makes no sense. Here is the code block and my explanation below.
<?php foreach($rows as $value): ?>
<?php echo $value['authorname'] . "<br />\n";?>
<?php echo $value['title'] . "<br />\n";?>
<?php echo $value['rating'] . "<br />\n";?>
<?php echo $value['imagelocation'] . "<br />\n";?>
<div class="block">
<div class="row">
<div class="col-md-4 col-md-8">
<div class="widget-block">
<input id="rate1" value="<?php echo $value['rating'];?>" type="number" class="rating" data-max="5" data-min="0" data-size="sm" data-show-clear="false" readOnly="readOnly">
<img class="img-responsive wow fadeInLeftBig animated" data-wow-duration="1.5s" src=<?php echo $value['$imagelocation'];?> alt=<?php echo $value['$authorname'];?>>
<br>
Buy this book
</div>
</div>
<div class="col-md-6 col-md-8">
<div class="section-sub-title">
<article class="section-title-body white">
<h1 class="head-title">Author: <span><?php echo $value['$authorname'];?> -</span> <?php echo $value['$title'];?></h1>
<span class="point-line hidden-xs hidden-sm"></span>
<p>
<?php echo $value['$review'];?>
</p>
</article>
</div>
</div>
</div>
</div>
<?php endforeach; ?>
The echo statements right after the start of the foreach loop prints out each variable fine. It isn't until we get down to the html that there are issues.
This code feeds an array of data from a DB then builds blocks of html code depending on the amount of data. In this case, I am pulling 8 records so this loop creates 8 copies of this code block. The thing that is frustrating is the variable "$rating" injects in all 8 blocks but none of the other variables do even though they print correctly on the page in the echo statements.
Maybe it is the data in the variable? For example as the code is parsed the first variable evaluated is $rating and works. The next one is the src property in the img tag $imagelocation and has an actual value of img\book_covers\TrueConviction.jpg
Are _ and . special characters and causing the issue? My return values would have _ . \ and spaces.
Thanks for any help.
I was right that it was the data IN the variables. The answer was urldecode. This solved my problems. I did this to all my variables.
$cleanauthorname = urldecode($value['authorname']);?>
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)
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]; ?>);
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
So I have the need to dynamically add html content using php which isnt the tricky part but I'm trying to put the HTML into a different location in the document than where the PHP is being run. So for example:
<div id="firstDiv">
<?php
echo "<div id=\"firstDivA\"></div>";
echo "<div id=\"secondDivA\"></div>";
?>
</div>
<div id="secondDiv">
</div>
But I want to be able to place the some HTML inside "secondDiv" using the PHP that is executed in the "firstDiv". The end result should be:
<div id="firstDiv">
<div id="firstDivA"></div>
</div>
<div id="secondDiv">
<div id="secondDivA"></div>
</div>
But I have no idea how to go about doing that. I read about some of the DOM stuff in PHP 5 but I couldn't find anything about modifying the current document.
You can open/close "blocks" of PHP wherever you like in your HTML
<div id="firstDiv">
<?php echo '<div id="firstDivA"></div>'; ?>
</div>
<div id="secondDiv">
<?php echo '<div id="secondDivA"></div>'; ?>
</div>
You can also capture the output if necessary with ob_start() and ob_get_clean():
<?php
$separator = "\n";
ob_start();
echo '<div id="firstDivA"></div>' . $separator;
echo '<div id="secondDivA"></div>' . $separator;
$content = ob_get_clean();
$sections = explode($separator, $content);
?>
<div id="firstDiv">
<?php echo $sections[0]; ?>
</div>
<div id="secondDiv">
<?php echo $sections[1]; ?>
</div>
Why not just move the relevant code to the right place?
<div id="firstDiv">
<?php
echo "<div id=\"firstDivA\"></div>";
?>
</div>
<div id="secondDiv">
<?php
echo "<div id=\"secondDivA\"></div>";
?>
</div>
The .php file is continuous thus if you have two separate <?php ?> tags they will be able to share the same variables.
<div id="firstDiv">
<?php
echo "<div id=\"firstDivA\"></div>";
$div2 = "<div id=\"secondDivA\"></div>";
?>
</div>
<div id="secondDiv">
<?php echo $div2 ?>
</div>
This will give the desired effect. (Demonstrates the use of variables)
I'm not sure what you're asking. Perhaps just add an echo statement to the second div.
<div id="firstDiv">
<?php echo "<div id=\"firstDivA\"></div>"; ?>
</div>
<div id="secondDiv">
<?php echo "<div id=\"secondDivA\"></div>"; ?>
</div>
Or do you mean you want to make DIV changes after PHP? Try jQuery!
Or do you mean you want to make DIV changes before PHP is finished? Perhaps phpQuery is good for you then.
If you want to work with XML-data (read XHTML), you'd rather use an appropriate XML processor.
DomCrawler is an excellent to work with DOM. It works with the native DOM Extension and therefore is fast and widely used.
Here an example from the doc on how to add content:
$crawler = new Crawler();
$crawler->addHtmlContent('<html><div class="foo"></div></html>');
$crawler->filter('div')->attr('class') // returns foo