I am trying to parse a html file using PHP Simple HTML DOM Parser, I am facing a div with a class which contains a space character
<div class="camera_src camerastarted">
<div data-thumb="/images/articles/football/th/css_sg_th.jpg" data- src="/images/articles/football/css_sg.jpg">
I used the following code:
$link = $html->find('.camera_src camerastarted div');
print_r($link);
foreach ($link as $ligne)
{
echo '-- ' . $ligne->getAttribute('data-src') . '<br />';
}
But I got nothing as display, only: Array()
What's wrong? thanks for advance!
You can select only one class like this
$link = $html->find('.camerastarted');
Have you tried this:
$link = $html->find('.camera_src.camerastarted');
Notice there is an error in your HTML, a space in "data- src", not "data-src"
Of course you get nothing.
Related
i have a codeigniter php website, in the image tag I have given an if statement to determine the source, the code is like below:
<img class="st_img" <?php if(str_replace(' ', '',$val['unique_tblkey'])=='entertainment'){?> src=" <?php echo ADMIN_IMG.strtolower(str_replace(' ', '',$val['unique_tblkey'])).'s/'.$val['image'][0]['btp_image'];?>"<?php} else { ?> src=" <?php echo ADMIN_IMG.strtolower(str_replace(' ', '',$val['unique_tblkey'])).'/'.$val['image'][0]['btp_image'];?> " <?php } ?>/>
I am getting
unexpected '}'
this error is coming although there isn't any unwanted brackets, can anyone please tell me what is wrong in my code, thanks in advance
OK, I finished rewriting it. Not completely sure if it will work, because I cannot test it.
<?php
$tableKey = strtolower(str_replace(' ', '', $val['unique_tblkey']));
$url = ADMIN_IMG .
$tableKey .
($tableKey == 'entertainment' ? 's/' : '/') .
$val['image'][0]['btp_image'];
echo '<img class="st_img" src="'.$url.'" />';
There's no repetition, multiple lines, and no mixing of PHP and HTML. This all makes for more readable code.
I want display my button if it have isset($_GET). I am trying to do like this.
<?php if(isset($_GET['project_id'])){
echo '<div class="add_btn_primary"> Project Users </div>';
}?>
its giving me error like
Parse error: syntax error, unexpected 'project_id' (T_STRING), expecting ',' or ';' in C:\xamppp\htdocs\mayank\add_project.php on line 101
I am not getting idea what I should do for echo project_id in div. Let me know if someone can help me for that.
Thanks
Thats incorrect to use echo inside another echo and how can you start a new php tag without closing the first.
The correct way is to concatenate the variable along the string passed in echo, here is how
<?php if(isset($_GET['project_id'])){
echo '<div class="add_btn_primary"> Project Users </div>';
}?>
instead of breaking the php tags break the ' quotes to concatenate the value in the string.
Why do you need again tag inside echo just use it as below:
<?php
if(isset($_GET['project_id']))
{
echo ('<div class="add_btn_primary"><a href="manage_project_users.php?project_id='.$_GET["project_id"].'>Project Users</a></div>');
}
?>
I'm writing an if statement in which a button needs to show if the cart is empty.
For this button I need to get the form key of the product for the data-url
So something like this:
Order
As mentioned above I need to wrap this button in an if statement, so something like this:
<?php
$_helper = Mage::helper('checkout/cart');
if (1 > $_helper->getItemsCount()){
echo 'Order';
}
else{
'<p>hello</p>';
}
?>
But obviously I can't have php echo within echo. Can anybody point me in the right direction of how to do this?
You don't put PHP inside HTML inside PHP. Since you're already in the context of PHP code, just concatenate the values you want to the output:
echo 'Order';
The resulting output is always just a string. You can simply build that string with whatever values you have.
You can just use string concatenation:
echo '<a href="#" data-url=".../' . Mage::getSingleton(...) . '"' ...
Simply don't open PHP up again. You can terminate the HTML interpretation inside an echo.
Your code should look like this:
<?php
$_helper = Mage::helper('checkout/cart');
if (1 > $_helper->getItemsCount()) {
echo 'Order';
}
else {
'<p>hello</p>';
}
?>
echo '<br />'.utf8_encode($article["texte_article"]);
I would like to have the text to use a css class "art". $article is a table, and texte_article is the selected column in the database.
echo '<br />''<div class=\"art\">'.utf8_encode($article["texte_article"])</div>;
It was a try but didn't work. Do you know why my syntax doesn't work/where I should put the class "art" ?
You are forgetting to add the end of the div in as a string, and you so it is messing up your php:
echo '<div class="art">'.utf8_encode($article["texte_article"]).'</div>';
There is an error in string concatenation. try this:
echo "<br /><div class='art'>".utf8_encode($article["texte_article"])."</div>";
I'm trying to make an image into a link using PHP and HTML. The main idea is to grab user's images and screen names from Twitter, then make the image into a clickable link to their profile by building the URL and adding their screen name on the end. But I get a error message:
Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ',' or ';' in C:\wamp\www\fyp\tweeter3.php on line 71.
This is line 71 (it's part of a foreach loop):
<?php echo "<img src = ".$userImage." class = ".$class.">"; ?>
There's a syntax error in there I just can't pinpoint.
These are my variables:
$userScreenName = $user -> screen_name;
$userImage = $user -> profile_image_url;
$class = "myImgClass";
$url = "https://twitter.com/".$userScreenName;
Can you spot the error?
You are missing a dot after $url and the HTML quotes to generate valid code:
<?php echo "<a href = '".$url."'><img src = '".$userImage."' class = '".$class."'></a>"; ?>
Without the quotes you get:
<a href = the url><img src = user image class = the class></a>
With the quotes:
<a href = 'the url'><img src = 'user image' class = 'the class'></a>
Missing . after $url:
<?php echo "<a href = ".$url"><img...
after $url you need to have a period.
Try this instead:
<?php
echo "<img src = \"".$userImage."\" class = \"".$class."\">";
?>
In my opinion, the simplest and most readable way to do is:
<?php echo "<a href = '$url'><img src = '$userImage' class = '$class'></a>"; ?>
There is only one long text, and no concatenation is used. It reduces the possibility to have an error caused by a missing double quote or missing dot. All PHP variables will be replaced automatically by their values.
You could also use printf to have all the variables outside of the string:
<?php printf('<img src = "%s" class = "%s">', $url, $userImage, $class); ?>
It is not a good practise to concatenate html string with the php variables. This leads to possible injection vector (XSS).
To avoid possible XSS (DOM OR STORED) please filter the variables as string. Specially if the value is from user input.
eg.
<?php echo "<a href = '".filter_var($url, FILTER_SANITIZE_STRING)."'