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>';
}
Related
<?php
echo get_menu_tree(0);
?>
Here's my PHP tag wherein I'm calling a function that displays all the data (text) from PHP MySQL Database.
The default color when I'm calling the function is white. How do I change the font color into Black?
I just updated my post. Here's my return function. Any ideas?
<?php
function get_menu_tree($parentID)
{
global $con;
$menu = "";
$sqlquery = " SELECT * FROM category where parentID='" .$parentID . "' ";
$res=mysqli_query($con,$sqlquery);
while($row=mysqli_fetch_array($res,MYSQLI_ASSOC))
{
$menu .="<li><a href='index.php?page=category&categoryID=".$row['categoryID']."'>".$row['name']."</a>";
$menu .= "<ul>".get_menu_tree($row['categoryID'])."</ul>"; //call recursively
$menu .= "</li>";
}
return $menu;
}
?>
Thank you!
<?php
function get_menu_tree($parentID)
{
global $con;
$menu = "";
$result = mysqli_query($con, "SELECT * FROM category WHERE parentID = '$parentID'");
while($row = mysqli_fetch_assoc($result)) {
$menu = "<li><a href='index.php?page=category&categoryID =".$row['categoryID']."'></a></li>";
}
$menu = "<ul>".$menu."</ul>";
return $menu;
}
?>
But if you want to output a table with the data i personaly would just use a php file and use this code
<?php
//Define $con here and put the php scripts you need in this file or use require(path/to/file.php) or include(path/to/file.php) to include other php documents
?>
//Some HTML...
<ul>
<?php
$result = mysqli_query($con, "SELECT * FROM category WHERE parentID = '$parentID'");
while($row = mysqli_fetch_assoc($result)) {
echo "<li><a href='index.php?page=category&categoryID =".$row['categoryID']."'></a></li>";
}
?>
</ul>
Let me be a little more generic instead of going straight to the point:
You can think at PHP as a lnaguage to manipulate text effectively and the kind of text you can manipulate is quite generic: in fact you can use PHP to produce any kind of text, also C, C++, ... code even if it is typically used in the Web context hence to produce HTML and CSS
As a result of this, PHP in itself has no understanding of the text it's manipulating, from its perspective it's just text
What you are interested in (color of an element) is something that is defined at CSS level, not at PHP level, however PHP is the tool you can use to produce CSS
To be slightly more complete (and conclude) this Appearance Semantic is defined at CSS level and requires HTML to define the structure first: the example provided by #TSteffenmann is in fact a specific case of this and it's hence correct but you can do something even more generic (i.e. instead of a span you can use p, ... whatever you want and use CSS to style)
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>';
}
?>
i would to know what is good practice for writing code to put all HTML code inside PHP function and in my front index.php file just call function to show code.
class.php:
public function test() {
$sql='select id,title from test ';
$nem=$this->db->prepare($sql);
$nem->execute();
$nem->bind_result($id,$title);
echo '<ul class="centreList">';
while($nem->fetch())
{
echo '<li>'.$id.'<a href="'.$title.'" >Download</a></li>';
}
echo '</ul>';
}
index.php:
<?php $connection->test(); ?>
This work fine, but I would like to know is this proper way or is not a good practice to use html code inside PHP functions?
It's ok to build HTML within PHP, but I would not echo to the screen directly from within the function. Instead, return the built HTML string.
$html = '<ul class="centreList">';
while($nem->fetch())
{
$html .= '<li>'.$id.'<a href="'.$title.'" >Download</a></li>';
}
$html .='</ul>';
return $html
The function should not be responsible for pushing content to the browser because it really limits what you can do with your code. What if you wanted to further process the HTML? What if you run into a condition later in the code and decided to abort? What if you wanted to set some response headers later? Some content would already be gone so none of these things would be possible without clever workarounds.
In general you want to separate your responsibilities: I would even break things down further:
one piece of code is in charge of retrieving info from the DB and returning
Another piece is in charge of building the HTML string
A third piece is in charge of displaying the HTML (probably your index.php)
New index.php
<?= $connection->test(); ?>
Do not use echo to print the html directly, wrap the html within while loop surrounded by php tags
public function test() {
$sql='select id,title from test ';
$nem=$this->db->prepare($sql);
$nem->execute();
$nem->bind_result($id,$title);
return $nem;
}
<ul class="centreList">
<?php $res = test()->fetch();
while( $res->fetch() ) { ?>
<li> <?php echo $id ?> Download </li>;
<?php } ?>
</ul>
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
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(' ', $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(' ', $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(' ', $level) . "<span style=\"text-decoration:underline;\">$name.$id </span><br>";
hope this helps