How to choose specific id from the array - php

How can I apply CSS underline to the following $name results where id=34,40 without using SQL query. Please help.
foreach($index[$parent_id] as $id) {
$name=$data[$id]["name"];
echo str_repeat('&nbsp', $level) . "$name.$id <br>";
}

Php is great at creating HTML and HTML can reference CSS without a thought.
If you have a CSS class called underline...
.underline {
text-decoration:underline;
}
...then all you need to do is wrap your output in a tag like this:
echo '<div class="underline">'.$name.$id.'</div>';
That said I'm not quite sure what you're trying to do with the str_repeat function, so you'd need to mod this to work with that.

You can apply a span around your output
echo str_repeat('&nbsp', $level) . "<span class="underline">$name.$id </span><br>";
and then style the class
.underline{text-decoration:underline;}
OR...
you can directly style it
echo str_repeat('&nbsp', $level) . "<span style=\"text-decoration:underline;\">$name.$id </span><br>";
hope this helps

Related

how do i reference a dynamically created list php

I have the following code to create category and subcategories from a
database
function displayChild($parent)
{
include 'connect.php';
$parentid=$parent;
$catSql='select* from categories where parentid='.$parentid.'';
$result=$con->query($catSql);
echo '<div><ul class='.'ul'.$parentid.'>';
while ($rows=$result->fetch_assoc())
{
echo '<li>'.mb_strtoupper($rows['catName']).'</li>';
displayChild($rows['catId']);
}
echo '</ul></div>';
}
displayChild('0');
The CSS bits should be as follows
#charset "utf-8";
.ul0{}
.ul1{}
.ul2{}
.
.
.
.uln{}
since the first tag appears outside the while loop it forms a really weird list when i reference it.putting it inside the while() loop is not an option either. please help
I'm not exactly sure what you mean by " it forms a really weird list when i reference it." However, it sounds like maybe you are getting odd html. If that's the case, you get better results if you put the nested function call inside the li:
function displayChild($parent){
include 'connect.php';
$parentid=$parent;
$catSql='select* from categories where parentid='.$parentid.'';
$result=$con->query($catSql);
echo '<div><ul class='.'ul'.$parentid.'>';
while ($rows=$result->fetch_assoc()){
echo '<li>'.mb_strtoupper($rows['catName']).'';
displayChild($rows['catId']); // note this is now inside the list item
echo '</li>';
}
echo '</ul></div>';
}

calling include function in echo statement

I am using whole code in php so i am using echo statement for displaying divs in html but how could i call php include function in echo statement to call a page see the code below:-
echo "<div style='position:relative;border:1px solid #A5BEBE;background-color: white;'>
include('botfunction.php');
</div>";
it is not calling that botfunction.php it just shows the text only as "include('botfunction.php');" inside the div tag .
Please tell me how can i call this php function without separating php and html code because the whole code is written in php which is impossible to separate.
Thanks
Remove the include from inside the echo.
echo "<div style='position:relative;border:1px solid #A5BEBE;background-color: white;'>";
include('botfunction.php');
echo "</div>";
which is why it's appearing as text instead of executing the include function.
This is going to be an absolute nightmare to maintain and if it is impossible to separate then I suggest rewriting from scratch. But as for your question, try this:
echo "<div style='position:relative;border:1px solid #A5BEBE;background-color: white;'>";
include('botfunction.php');
echo "</div>";
You just need to move the include out of the echo string otherwise it will be evaluated as a string instead of a function
echo "<div style='position:relative;border:1px solid #A5BEBE;background-color: white;'>";
include('botfunction.php');
echo "</div>";

PHP script inside of string?

Is this possible?
What I am trying to accomplish:
Create a html template in the form of a string
Inside the string, add a script, something like include 'php/countries.php';
Echo entire string to html page
Everything but the 2nd step works. I would like to see a php file echo the question being asked, onto the html page, including an echo from another php file.
EXAMPLE
echo "<div id=\"first\"><?php include \'countries.php\'; ?></div>";
I have tried the above, as well as the below:
EXAMPLE
echo "<div id=\"first\">".include 'countries.php'."</div>";
Would this require eval?
Any and all help is appreciated.
Seems a bit silly, but you could do the following:
echo "<div id=\"first\">" . file_get_contents('countries.php') . "</div>";
Or...
echo "<div id=\"first\">";
include "countries.php";
echo "</div>";
Or...
$externalfile = compileexternal('countries.php');
function compileexternal($file) {
ob_start();
require $file;
return ob_get_clean();
}
echo "<div id=\"first\">" . $externalfile . "</div>";
If none of these are what you need, please update the question. There are a dozen ways.
You can use
eval()
But it is not a good practice.
You can use a regular expression.
For example, your string could be;
<div id="first">{{countries.php}}</div>
You'd then do;
$string = "<div id='first'>{{test2.php}}</div>";
echo preg_replace_callback("/(\{\{.+\}\})/", function($matches) {
include_once( str_replace(array("{", "}"), "", $matches[0]));
}, $string);
Check the file exists if( file_exists() )
Check the file can be included (we don't want to include ../../../../../etc/passwd

PHP - Echoing a variable in color

Im new to learning PHP as you might have guessed. I have the contents of a .txt file echoed but I would like it to stand out more, so I figured I would make it a different colour.
My code without colour:
<?php
$file = fopen("instructions.txt", "r") or exit("Unable to open file");
while(!feof($file))
{
echo fgets($file);
}
fclose($file);
?>
I have researched this and seen suggestions to others to use a div style, however this didn't work for me, it gave me red errors all the way down the page instead! I think its because I'm using 'fgets' not just a variable? Is there a way to colour the echo red?
The code I tried but doesn't work:
echo "<div style=\"color: red;\">fgets($file)</div>";
(In general) You need to separate the actual PHP code from the literal portions of your strings. One way is to use the string concatenation operator .. E.g.
echo "<div style=\"color: red;\">" . fgets($file) . "</div>";
String Operators
Other answer already told that you can't use a function call in a double quoted string. Let additionally mention that for formatting only tasks a <span> element is better suited than a <div> element.
Like this: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/span
You should try:
<div style="color: red;"><?= fgets($file);?></div>
Note: <?= is an short hand method for <?php echo fgets($file);?>
This version does not need to escape double quotes:
echo '<div style="color:red;">' . fgets($file) . '</div>';
You can do this with the concatenate operator . as has already been mentioned but IMO it's cleaner to use sprintf like this:
echo sprintf("<div style='color: red;'>%s</div>", fgets($file));
This method comes into it's own if you have two sets of text that you want to insert a string in different places eg:
echo sprintf("<div style='color: red;'>%s</div><div style='color: blue;'>%s</div>", fgets($file), fgets($file2));

div inside php not worrking

I am trying to style a sentence within php by trying to echo the div. However the styling is not being applied. The css is correct, I think I have something wrong on how I am calling a div withing php. Can you please advise.
This what I have tried:
echo '<div id="sumtitle">' . "<tr><td>" . "This is a test" . "</td></tr>" . '</div>';
tried also like this:
echo '<div id="test">Test sentence</div>';
thanks
PHP is not responsible for styling.
You have to check your styles file or whatever.
Check your HTML and CSS.
PHP has nothing to do here.
The code looks OK, although the multiple use of the . in the first is unnecessary. You could just put:
echo '<div id="sumtitle"><tr><td>This is a test</td></tr></div>';
BTW: Are you using a <table> for your <tr> and <td>?
Regardless, the styling should work fine if you use something like:
#sumtitle { font-style: italic; }
or
#test {...}
I'd double check the CSS! :)

Categories