Echoed variable displayed outside of parent div
First of all, I'm not a PHP geek and I am just tampering with this for fun.
I have a function that loops through categories in my virtuemart based shop (localhost only) and creates a <select>
object for all the categories with subcategories; the category "shoes" has the subcategories "boots" and "sandals," so a <select>
with these two options is created.
Now the result of this function is stored in a variable ($v2) and then echoed along with a different variable in a form, as in:
<div id="addwrap">
<?php
echo $vl;
echo $v2; ?>
</div>
All works well, but $v2
is displayed outside of the "addwrap" element.
The function used has no DIV elements that could remain open and rip the page apart.
Do you have any idea on why this is happening?
Edit - the resulting HTML looks like this:
<select id="kat244" class="inputbox" name="list_order">
<option value='one'></option>
<option value='two'></option></select>
<div id="addwrap">v1 content</div>
The code snippet I posted in the comments was from firebug - that's why it's displayed wrong
Edit nr2 - I can't get the hang of this editor.. :\ The code is now the code displayed. Sorry about the errors... :\
Final edit - posting complete function code related to the rendered snippet:
T开发者_C百科his is the main function - calls for two other functions (has_childs and list_level_mod) posted below `function trown($class="",$category_id="0", $level="0") { $db = new ps_DB;
$level++;
$q = "SELECT * FROM #__{vm}_category,#__{vm}_category_xref ";
$q .= "WHERE #__{vm}_category_xref.category_parent_id='";
$q .= $category_id . "' AND ";
$q .= "#__{vm}_category.category_id=#__{vm}_category_xref.category_child_id ";
$db->setQuery($q);
$db->query();
while ($db->next_record()) {
if ( $this->has_childs( $db->f("category_id")) == true)
{
echo $this->list_level_mod( $db->f("category_id"),'1','0');
}
else
{
continue;
}
$this->trown($class, $db->f("category_child_id"), $level);
}
}`
This is the function that checks whether a category has subcats:
function has_childs($category_id) {
$db = new ps_DB;
if( empty( $GLOBALS['category_info'][$category_id]['has_childs'] )) {
$q = "SELECT category_child_id FROM #__{vm}_category_xref ";
$q .= "WHERE category_parent_id='$category_id' ";
$db->setQuery($q); $db->query();
if ($db->num_rows() > 0)
$GLOBALS['category_info'][$category_id]['has_childs'] = true;
else
$GLOBALS['category_info'][$category_id]['has_childs'] = false;
}
return $GLOBALS['category_info'][$category_id]['has_childs'];
}
And this is the function that renders the select objects:
function list_level_mod( $category_parent_id, $category_id='0', $list_order=0 ) {
$db = new ps_DB;
if (!$category_id) {
return $GLOBALS['VM_LANG']->_('CMN_NEW_ITEM_LAST');
}
else {
$q = "SELECT list_order,category_id,category_name,category_child_id FROM #__{vm}_category, #__{vm}_category_xref ";
$q .= "WHERE category_parent_id='$category_parent_id' ";
$q .= "AND category_child_id=category_id ";
$db->query( $q );
$html = "<select id=\"kat".$category_parent_id."\">\n";
while( $db->next_record() ) {
$html .= "<option value=\"".$db->f("category_id")."\" >".$db->f("category_name")
."</option>\n";
}
$html .= "</select><br/>";
return $html;
}
}
Finally, results of the main function are passed to the $v2 variable and than echoed as posted above.
try giving
<div id="addwrap">
<?php
echo $vl;
echo $v2; ?>
<div style="clear:both;"></div>
</div>
I think the above problems should have solved your problem. To my opinion, are you styling CSS. If so, have you checked your style?
#addwrap{
padding:20px;
overflow:hidden;
}
Forgive, if this doesn't help. It's just another idea.
Ok... figured it out thanks to a friend.
The problem was the echo part in the main function:
echo $this->list_level_mod( $db->f("category_id"),'1','0');
and the fact that the results of the function were not passed to $v2
Corrected, working properly function:
function trown($class="",$category_id="0", $level="0") {
$db = new ps_DB;
$level++;
$q = "SELECT * FROM #__{vm}_category,#__{vm}_category_xref ";
$q .= "WHERE #__{vm}_category_xref.category_parent_id='";
$q .= $category_id . "' AND ";
$q .= "#__{vm}_category.category_id=#__{vm}_category_xref.category_child_id ";
$db->setQuery($q);
$db->query();
$html = "<div id=\"selekty\">";
while ($db->next_record()) {
if ( $this->has_childs( $db->f("category_id")) == true)
{
//$this->list_level_mod( $db->f("category_id"),'1','0');
$html .= $this->list_level_mod( $db->f("category_id"),'1','0');
}
else
{
continue;
}
$html .=$this->trown($class, $db->f("category_child_id"), $level);
}
$html .= "</div>";
return $html;
Thank you all for your time, Shh
精彩评论