better way with Simple HTML Dom Parser [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
my HTML code is THE CODE IS REPEATED 16 times :
<div class="headline_image">
<a ga-cat="slideshow-view" ga-action="view-1" href="mylink"><img src="http://dd4994.jpg" width="420" height="323" align="right" alt="my text "/></a>
</div>
I WANT TO GET all the imgs links and text also href what i did :
for ($x = 0; $x <= 15; $x++) {
$imglink = $html->find('div[class=headline_image] img', $x)->getAttribute('src');
$mytext = $html->find('div[class=headline_image] img', $x)->getAttribute('alt');
$postlink = $html->find('div[class=headline_image] a', $x)->getAttribute('href');
echo '<br/>';
echo $mytext;
echo '<br/>';
print_r($postlink);
echo '<br/>';
}
the code is slow any changes ?

You slow down your code by using too much anonymous objects. It means you don't put the result of the function into a variable, rather just use it "on the go". This needs to run your function again and again slowing down your project.
Because you can use the function find to return an array, I advice you to do so before the for loop.
$imgarray = $html->find('div[class=headline_image] img', $x);
This way you run $html->find exactly once, and not sixteen times. In the for loop you can use it as an array and work with the results: $imgarray[$x]. You make the same for $anchorarray and your code will speed up, you'll see.
Alternative solution is using PHP DOM $childNodes on the container in which this 16 item can be found (or the body element). This will return the sixteen div elements in which you can navigate by calling $firstChild for the <a> element and $firstChild again for the <img> element. Probably this is more secure in case you want to make changes to the website (like adding more content to the end etc.)

Hey Daniel i changed the code to :
$imgarray = $html->find('div[class=headline_image] img');
$linkarray = $html->find('div[class=headline_image] a');
for ($x = 0; $x <= 15; $x++) {
echo $imgarray[$x]->getAttribute('src');
echo '<br/>';
echo $imgarray[$x]->getAttribute('alt');
echo '<br/>';
echo $linkarray[$x]->getAttribute('href');
echo '<br/>';
}

In general the proper way to iterate looks like this:
foreach($html->find('div') as $div){
echo $div;
}

Related

How to create dynamic color for text? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a variable which is containing a number. Now I want, if the number is positive, then its color be green, and if that number is negative, then the color of it be red. Something like this:
<?php
$var1 = 5;
$var2 = -2;
?>
<div> <?php echo $var1; ?></div>
<div> <?php echo $var2; ?></div>
It should be noted, in reality there is just one variable and it can be both positive and negative.
Also I want this result:
<div style='color:green;'>5</div>
<div style='color:red;'>-2</div>
For example, use an inline ternary operator in PHP. (It's like an If statement)
<div style='color: <?php echo ($var1 > 0 ? 'green' : 'red') ?> ;'>5</div>
<div style='color: <?php echo ($var1 > 0 ? 'green' : 'red') ?> ;'>-2</div>
Since you have a lot of numbers, why not just use a simple function? Also I would recommend using classes instead of inline styles as that is generally preferred:
function getNumberHtml($num) {
$class = ($num < 0) ? 'negative' : 'positive';
return "<div class='$class'>$num</div>";
}
echo getNumberHtml(56);
echo getNumberHtml(-5);
Then just add whatever you want to your CSS file. Edit your styles accordingly:
.negative {
color: red;
}
.positive {
color: blue;
}

php - How do I add div in a php function? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am trying to add a <div> to my php, but everything I've found on google hasn't worked for me. I don't know what I'm doing wrong. Any ideas on how to add a <div> in a php function for styling purposes only?
<?php
//get_additional is a custom function
ob_start();
echo get_additional('Cosmos','Trades');
echo nl2br("\n");
//I want the div to start here
$buffered = ob_get_clean();
echo $buffered;
$times = $buffered;
$imgurl = 'Images/cat.png';
for($i=0;$i<$times;$i++){
echo '<img src="'.$imgurl.'" />';
}
?>
You need to echo it.
Like you've already used once in your code, echo '<img src="'.$imgurl.'" />';
Now coming back to your code:
<?php
//get_additional is a custom function
ob_start();
echo get_additional('Cosmos','Trades');
echo nl2br("\n");
//I want the div to start here
echo '<div class = "blah">
// whatever you want to do here
</div>';
$buffered = ob_get_clean();
echo $buffered;
$imgurl = 'Images/cat.png';
for($i=0;$i<$times;$i++){
echo '<img src="'.$imgurl.'" />';
}
?>

Creating a variable template to display a database content

I'm working on a database website currently and part of the project is to display the content of a database in a custom form using pixels to position the various fields.
I am however having trouble getting the code to work as the way I'm creating the form doesn't seem to work in a php tag, but It requires a WHILE loop in order to continue running until the database field is empty.
I'm only including the relevant code section so as to not clog up the post, all my coding is segmented so I can isolate bugs.
<?php
$usernameh = 20;
$units = "px";
for ($x=0; $x<=2; $x++) {
$pix=$value . $units;
<div style="position:absolute;left:10px;top:20px;width:100px;height:20px;z- index:5;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:15px;">Field Name:</span></div>
$value += 150;
}
?>
The idea is to substitute the 20px on the first line of the divcommand with $pix so that with each iteration the value increases by a set amount and separates out the entries.
I'm very new to php coding, as in only really started 2 weeks ago. I'm sure there is a simple solution to this but I'm not sure what question to ask Google to get the response I'm after.
I hope my problem makes sense. The database is in MySQL but all that is fine, its just the formatting I'm struggling with. Even without using a variable in the formatting code the script crashes while the php tag is in effect.
Can anyone offer any advice on where my problem is or suggest another alternative to this.
Thanks!
You need to break out of PHP ?> before HTML and then switch back to PHP <?php before more PHP code
$value = 20;
$units = "px";
for ($x=0; $x<=2; $x++) {
$pix = $value . $units;
?>
<div style="position:absolute;left:10px;top:<?php echo $pix ?>;width:100px;height:<?php echo $pix ?>;z- index:5;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:15px;">Field Name:</span></div>
<?php
$value += 150;
}
Or for the HTML you can echo it from PHP:
echo '<div style="position:absolute;left:10px;top:' . $pix . ';width:100px;height:' . $pix . ';z- index:5;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:15px;">Field Name:</span></div>';

Returning a string that has been span styled in a function [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
My function is supposed to accept two parameters: $message, which is just a string of words, and $color, which is a string with a color name.
I want to make it so that the color in variable $color is added to $message and the final result returned would be a span style with the color applied to the message. I've been looking online for a solution but have had no luck. Here is the code so far. What am I missing?
function speak($message,$color) {
//supposed to convert $message into <span style='color:$color;'>$message</span>
return "$message$color";
};
//red as the span style color
$red="red";
//message that will accept span style
$mess="Hello World";
//invoke function speak to style them both
$total=speak($mess,$red);
//output total
echo $total;
As mentioned, the comment in your code already says what the function should do:
//supposed to convert $message into <span style='color:$color;'>$message</span>
So, let's turn that into an actual statement:
return "<span style='color:$color;'>$message</span>";
Woah, not so fast! You're supposed to return valid HTML, so you must properly escape the variables; there are various ways to do this, but I prefer to use sprintf():
return sprintf('<span style="color: %s;">%s</span>',
htmlspecialchars($color, ENT_QUOTES, 'UTF-8'),
htmlspecialchars($message, ENT_QUOTES, 'UTF-8')
);
See also: htmlspecialchars()
Btw, inline CSS should be avoided; more often than not, classes are the preferred approach.
Try this one:
You only need to put this line of code into a variable or directly return it:
$mes = "<span style='color:$color;'>$message</span>";
Code:
function speak($message,$color) {
//supposed to convert $message into <span style='color:$color;'>$message</span>
$mes = "<span style='color:$color;'>$message</span>";
return $mes;
};
//red as the span style color
$red="red";
//message that will accept span style
$mess="Hello World";
//invoke function speak to style them both
$total=speak($mess,$red);
//output total
echo $total;
Here it is. Like you said, you're missing something easy:
function speak($message,$color) {
return "<span style='color:$color;'>$message</span>";
};
<?php
function speak($message, $color) {
return <<<END_OF_HTML
<span style="color:$color;">$message</span>
END_OF_HTML;
}
echo speak("hello", "blue");
?>
--output:--
<span style="color:blue;">hello</span>

joomla 2.5 navigation menu subtitle [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
Hi I am web designer and I would like to know how to set menu subtitle? It is easily possible with yoo themes but I need to know how it is done without using YooTheme templates. I think there is need of little modification in mod_menu but I don't know what exactly. I googled all day and can't find a solution.
There are certainly better solutions but i've done it this way:
Insert a charakter in the name of your menu-item. For example a "|".
It should look like this: Title | Subtitle. At this position you can divide the name.
Now you have to override the file default_component.php in modules/mod_menu/tmpl.
Add this lines:
$parts = explode("|", $linktype);
// the "|" is the divider
if(isset($parts[1])){
$linktype = $parts[0].'<span>'.$parts[1].'</span>';
}else{
$linktype = $parts[0];
};
after:
$class = $item->anchor_css ? 'class="'.$item->anchor_css.'" ' : '';
$title = $item->anchor_title ? 'title="'.$item->anchor_title.'" ' : '';
if ($item->menu_image) {
$item->params->get('menu_text', 1 ) ?
$linktype = '<img src="'.$item->menu_image.'" alt="'.$item->title.'" /><span class="image-title">'.$item->title.'</span> ' :
$linktype = '<img src="'.$item->menu_image.'" alt="'.$item->title.'" />';
}
else { $linktype = $item->title;
}
Now you have a span around the subtitle and it's possible to style it.

Categories