Using Xpath, I'm extracting some data from an HTML page. I've the below portion in my code:
echo '<ol type="A">';
foreach ($options as $option) {
echo '<li>'.$option->nodeValue.'</li>';
}
echo '</ol>';
Result is
A. some text
B. some other text
C. a different text
D. yet another text
I want to manually copy the output to a text file. When I do that, the prefixes (A., B.,C,D.) are not copied from browser to text file. So I want to add the prefixes A,B,C,D to my $option->nodeValue inside the foreach loop (always 4 members in the array)- instead of using li tag. How can I do that? (or is there a way to simple copy from browser with the li tag output :) )
YOu mean like this?
$letters=['A','B','C','D'];
echo '<ol type="A">';
foreach ($options as $index=>$option) {
echo '<li>'.$letters[$index] . " " . $option->nodeValue.'</li>';
}
echo '</ol>';
Related
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>';
}
?>
Faced a problem when working on .xml file parsing in PHP
What can i put in the ancher tag in that case :
<?php
foreach ($xml->issue as $issue ) {
echo '<a name="$issue->id"></a>';
//rest of code
}
<a href="# "> //i dont know what to put after the "Diese".
?>
i want to scroll to an element inside the loop .
First, your <a href> tag is empty. And because of that, you won't see any links on your page:
TEXT HERE!!!
Second, in PHP, it is not valid to introduce variables in single quoted strings, like you did. Iterpreter will not recognize them as variables and therefore will not convert them to their values. My solution (take a look at quoting style):
foreach ($xml->issue as $issue ) {
echo '<a name="'.$issue->id.'">some link</a>';
}
You are using quotes and double-quotes wrong.
<?php
foreach ($xml->issue as $issue)
{
// This will work - '.$var.'
echo '';
// And this
echo "";
// And this
echo "";
// These will fail
echo '';
echo '';
echo '';
}
?>
My PHP code generates the elements of class="error". I want all such generated elements to lie within the "class1" div tag for which I need to know when to close it by "
<?php
foreach ($myArr as $i => $value) {
$id = some_function($value);
if ($id == NULL ) {
print "<div class="error"><b>$value</b> could not be processed.</div>"
}
} else { ...
}
?>
<div class="class2">
</div>
Now, I don't know when to put the last tag to close ... Solution1: I could always search my array for the last "NULL" value and for that that key alone I could close the tag, but Is there any other better trick with which I can assign all the elements that are generated to go under a particular tag?
Thanks,
John
You should try something like that to put the div closing tag:
foreach ($array as $i => $value)
{
$id = some_function($value);
if ($id == NULL ) {
echo "<li><b>".$value"</b> could not be processed.</br></li>";
} else {
...
}
if ($i == end(array_keys($array))){ // last element of array
echo "</div>";
}
}
For your second question, if you want to assign certain elements to go under a particular tag, PHP is not done for that. Of course you can do it, but manually.
You should use Javascript (with JQuery http://api.jquery.com/append/) to append content easily in elements (don't know if that correspond to what you need).
I have a script which loads items from an XML file, and displays them, so a user can choose which item they want to remove from their file. Here's the PHP:
<?php
global $current_user;
get_currentuserinfo();
$userid= $current_user->ID;
$dom = new DOMDocument;
$dom->load("playlists/$userid.xml");
echo '<div class="styled-select">';
echo '<center><form name="input" action="/remove/removesure.php" method="get">';
echo '<select name="q[]" size="2" multiple>';
$titles = $dom->getElementsByTagName('title');
foreach ($titles as $title) {
echo '<option>'.$title->nodeValue.'</option>';
}
echo '</select>';
echo '<input type="submit" class="submit" value="Remove">';
echo '</form></center>';
echo '</div>';
?>
The problem I've ran into is that it doesn't display some objects correctly, mainly items with hyphens (it displays – instead of -) and titles with spaces at the end, and because of this, my removal code doesn't find the item, and so can't remove it. I don't know what to do, and I don't know why it's doing this. I'm running the code in wordpress, if that makes a difference.
Any ideas?
If there is no chance of having any kind of councurrency, I would suggest you to use the title index of the title tag as value of "option". Eg:
$titles = $dom->getElementsByTagName('title');
$counter = 0;
foreach ($titles as $title) {
echo '<option value='.$counter.' >'.$title->nodeValue.'</option>';
$counter++;
}
In removesure.php, you could handle this by using an XPath expression like the next one:
//Title[2]
where "2" is the index of the title that must be removed.
That is a possible solution; another path you could try to follow is to handle spaces providing the best encoding option for your titles. htmlentity is the function that you should execute
echo '<option>'.htmlentity($title->nodeValue).'</option>'
Here's the problem, I am trying to echo a statement or an array after dynamically generated HTML, and unfortunately the thing that i want to echo goes above the HTML, is there any way to echo it after that dynamic HTML or work around?
Code:
Link 1
Link 2
if(isset($_GET["id"]) && $_GET["id"] == "do_something") {
$html = "dynamic html generate";
echo $html;
//after this im using foreach
foreach($array as $item) { echo $item . "<br />"; }
}
As I click one of these two , dynamically generated HTML shows up. Now for example I have an array:
$array = array("error1", "error2");
All the generated PHP goes above the dynamic HTML :/.
How should i fix it so that i can echo all of this array below the dynamic HTML?
Thanks
Use buffering with ob_start
ob_start();
// dynamic html code generate
$dynamic_html = ob_get_clean();
echo $dynamic_html;
// your code
echo $dynamic_html;
Sounds like you missed some closing tags (most likely </table>) in the dynamic html. Thats why the later generated echo gets displayed at the top.
Example (Note the missing closing table):
<?php
echo "<table><tr><td>TableText</td></tr>";
echo "I should be bellow the table, but going to the top.";
?>
will produce:
I should be bellow the table, but going to the top.
TableText