PHP - echo inside an echo - echo

I have a PHP if/else statement. This is the code I'm trying to echo under an else condition.
<?php $locked = ForumData::is_topic_locked($post->topic_id);
if ($locked->topic_locked == 1) {echo '<td align="right"><font color="#FF0000">Topic Locked</font><td>';}
else {
echo '<td align="left"><img src="<?php echo SITE_URL?>/lib/skins/flyeuro/images/forums/t_reply.gif"/></td>'; }
?>
The bit I'm interested to echo is this.
<img src="<?php echo SITE_URL?>
If I try this... 'echo SITE_URL'
Parse error: syntax error, unexpected T_ECHO, expecting ',' or ';'
But this doesn't parse the image, and if I try parsing anything else, it's giving me parsing errors, which I can't fix?
How can I therefore produce an echo inside another echo?

why did you open a <?php tag again, you are already in echo line?
echo '<td align="left"><img src="'.SITE_URL.'/lib/skins/flyeuro/images/forums/t_reply.gif"/></td>';
and what is SITE_URL? Is that a variable, did you forget to put $?
echo prints out the string that you gave as parameter,
echo "foo";
As #hakre mentioned about it, . is used to concatenate strings.
$var = "foo"."bar"; //foobar
So you can use it in echo line,
$var = "foo"."bar"; //foobar
echo "foo "."bar ".$var // foo bar foobar
And It's not important weather variable defined as a string. It would be a constant variable.
define('SITE_URL', 'localhost:8080/phpvms');
echo "my website URL is ".SITE_URL; //my website URL is localhost:8080/phpvms

Remember:
<?php echo "View"; ?>
" and \
this!
Hope that's enough of a hint!#

Your problem is probably solved this way:
echo '<td align="left"><a href="',
url('Forum/create_new_post?topic_id=' . $post->topic_id . '&forum_id=' . $post->forum_id . '') ,
'"><img src="', SITE_URL,
#######################
'/lib/skins/flyeuro/images/forums/t_reply.gif"/></a></td>';
In PHP you can use constants quite like variables, e.g. to output them. You don't need to stack echoes inside each other or something.

Related

Sql-injection error

I am trying to do SQL-injection attack on a local website on my localhost. I am trying to get all the products from product table using the wildcard ';-- but there seems to be some problem with the query. It's giving me this error
'Warning: mysqli_fetch_assoc() expects parameter 1 to be
mysqli_result, boolean given in C:\wamp64\www\tplus\products.php on
line 151'
Here is my PHP code
<?php
//$search_value = mysqli_real_escape_string($conn,$_GET['search']);
$search_value = $_GET['search'];
$result = mysqli_query($conn,"SELECT * FROM products where p_name LIKE '%".$search_value."%'");
while ($row = mysqli_fetch_assoc($result))
{
echo "<tr>";
echo "<td>";
echo $row['p_name'];
echo "</td>";
echo "<td>";
echo $row['p_price'];
echo "</td>";
echo "<td>";
echo $row['p_brand'];
echo "</td>";
echo "<td>";
echo $row['p_info'];
echo "</td>";
echo "</tr>";
}
?>
Try this
if (!$result) {
die(echo 'MySQL Error: ' . mysqli_error())
}
You need to check if
"SELECT * FROM products where p_name LIKE '%".$search_value."%'"
is a valid SQL statement.
The indication is that it is not - hence the error.
Perhaps output that string and check where the SQL statement is incorrect.

Why preg_replace() function isn't working properly?

My PHP Script is:
<?php
$string = '{controller}/{action}';
$pattern = '/\{([a-z]+)\}/i';
$replacement = '(?P<$1>[a-z-]+)';
echo preg_replace($pattern, $replacement, $string);
?>
it is showing this result:
(?P[a-z-]+)\/(?P[a-z-]+)
I am expecting this:
(?P<controller>[a-z-]+)\/(?P<action>[a-z-]+)
How I can able to do this??
Your code produces the correct result, that is,
(?P<controller>[a-z-]+)\/(?P<action>[a-z-]+)
The problem is: when you echo that out and display it in a browser, the browser interprets <controller> and <action> as HTML tags, like <p> or <strong>. So, it doesn't display them; it only displays what is left:
(?P[a-z-]+)\/(?P[a-z-]+)
You would see the correct result if you ran this script from the command line. To make it work in the browser, you need to replace the last line with
echo htmlentities(preg_replace($pattern, $replacement, $string));

how to replace all non php script by adding "echo"

I want to replace all non php script by adding "echo".
Using regex and replacement can be multiline.
example:
<h1> heading 1 </h1> ==> echo '<h1> heading 1 </h1>';
<script> ..... </script> ==> echo '<script> ..... </script>';
Can anyone help?
Try this :
<?php
$str="<h1>Hello world</h1>";
echo preg_replace("/(.+)/i","echo '$1'",$str);
?>
(.+) is a capture group it captures the entire string and saves it for reuse in replacement perameter as $1.
Live Demo :
https://eval.in/498724

Default checked checkboxes undefined in php

I am having trouble with checkboxes. What I am doing is displaying a list of checkboxes, if previously checked they will show the check mark, then you submit them and another php should recognize which were checked and which weren't. My script works fine for boxes previously unchecked, if you check them the action php recognizes it, but for boxes already checked I get Notice - undefined variable - for the boxes (even if unchecked/checked again). I really can't seem to find my way around this.
My code is
$ind=0; //counting variable
//generating checkboxes from an xml
foreach($xml as $checkbox)
{
$checks=$xml->checkbox[$ind]->active; //the active tag has a 0 or 1 stored.
echo "Activate ".$ind; // shows activate 0, activate 1, etc...
echo "<form name='checkb' action='show.php' method='post'>
echo "<input type='checkbox' name='checks[]' class='act' value='".$ind."'";
if($checks==0){ echo ">";} else{echo " checked ='checked'>";}
echo "<input type='hidden' name='ind' value=".$ind.">";
$ind=$ind+1;
echo "<input type='submit' name='sub' value='Submit'/> </form>"; }
On my action php I have
$chks = $_POST['checks'];
$N = count($chks);
echo("Active checkboxes ");
for($i=0; $i < $N; $i++)
{
echo($chks[$i] . " ");}
All this worked well until I decided to show if the boxes had been previously checked. So I guess the question is, why won't php recognize checked=checked as a true value? Or is there any other way to do this?
Thanks!
Seems like a lot of issues here.
Why are you outputting a form for each checkbox?
Where is your submit for the form?
You need a space in front of checked='checked' where you echo it out - echo " checked='checked'>"
If you move form output out of the loop, you will need to add your incremented value to the hidden input name property as well (or make it an array like checks, otherwise you will
only get one value for the field.
My attempt to try and clean it up a little.
$ind = 0;
echo "<form name='checkb' action='show.php' method='post'>";
foreach($xml as $checkbox){
echo "Activate $ind";
echo "<input type='checkbox' name='checks[]' class='act' value='$ind' ".(($checks == 0) ? " />" : " checked='checked' />";
echo "<input type='hidden' name='ind_$ind' value='$ind' />";
$ind++
}
echo "</form>";
Notice, like Mike said before that checked='checked' has the space before so that there is seperation between components. Also, your hidden elements all had the same names, so I added the $ind quantifier.
Hope this helps.

Zend_Navigation rendering submenu with partial

I've posted an edit to my question. While working on it I noticed the problem is easy to simplify.
I need a custom format of my submenu so i have to use partial. But then the problem occurs.
The below code shows the INCORRECT level (0):
echo $this->navigation()->menu()
->setMinDepth(1)
->setMaxDepth(1)
->setRenderParents(false)
->setOnlyActiveBranch(true)
->renderPartial(null, array('partials/menu.phtml', 'default'));
The below code shows the CORRECT menu level (1)
echo $this->navigation()->menu()
->setMinDepth(1)
->setMaxDepth(1)
->setRenderParents(false)
->setOnlyActiveBranch(true)
->render();
Any ideas? Guys please. I would appreciate any help!
Edit
My partials/menu.phtml:
foreach ($this->container as $page)
{
$active = $page->isActive();
echo '<div class="item">';
echo '<a class="'. ($active ? 'active' : '') .'" href="' . $this->baseUrl($page->getHref()) . '">' . $page->getLabel() . '</a>';
echo '</div>';
}
EDIT 2
My understanding of Zend_Navigation was, first to prepare container and than put it through partial.
$nav = $this->navigation()->menu()->setOnlyActiveBranch(true)->getContainer();
echo $this->navigation()->menu()->renderPartial($nav, array('/partials/menu.phtml', 'default'));
What is the point of setting set{Min/Max}Depth, parentRendering at the container when passing it anywehere is useless?
I use this code:
<?=$this->navigation()->menu()->renderPartial(null, 'shared/menu.phtml')?>
you should pass true to the method $page->isActive(true) so that also functions in depth.
in your partial
foreach ($this->container as $page) {
$active = $page->isActive(true);
if (count($page->getPages())) {
foreach ($page->getPages() as $subPage) {
$active = $subPage->isActive(true);
echo '<div class="item">';
echo '<a class="'. ($active ? 'active' : '') .'" href="' . $this->baseUrl($subPage->getHref()) . '">' . $subPage->getLabel() . '</a>';
echo '</div>';
}
}
}
before the second foreach you could add a check if and when to show the submenu.
my 2 cent.
EDIT
try this:
$partial = array('partials/menu.phtml', 'default');
echo $this->navigation()->menu()
->setMinDepth(1)
->setMaxDepth(1)
->setRenderParents(false)
->setOnlyActiveBranch(true)
->setPartial($partial)
->render();
Came across this while searching for an answer to the same problem. Having looked through the code for Zend_View_Helper_Navigation_Menu, it doesn't look like any of the view helper options are passed through to the view partial, although I don't see why they couldn't be... (in ZF 1.12 take look a line 736 of Zend_View_Helper_Navigation_Menu, the only thing passed is the container itself, the options array could easily be passed along with it, or the container prefiltered, may be worth filing a feature request with ZF)
These options are purely a way of filtering the Zend_Navigation_Container for rendering with the default renderMenu method. As you say, it seems you can accomplish the same thing by first filtering the container and then passing it as the first argument of the renderPartial method
In your main view
Find the container of the submenu located in navigation config. Then echo this container using said partial.
$pages = $this->navigation()->findOneBy('label', 'Label of your submenu');
echo $this->navigation()->menu()->renderPartial($pages,module/partials/menu.phtml');
In the partial (module/partials/menu.phtml)
Customise. This example iterates over the top level pages of your chosen container.
foreach ($this->container as $page) {
echo $this->navigation()->menu()->htmlify($page) . PHP_EOL;
}