The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
Problem with Div's
Discuss Problem with Div's in the PHP Development forum on Dev Shed. Problem with Div's PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 10th, 2013, 07:10 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 5
Time spent in forums: 2 h 21 m 10 sec
Reputation Power: 0
|
|
|
Problem with Div's
I'm trying to create div's dynamically, say every 5 rows. I can do this, but not when I add the rest of the code i'm using (menu script). My code is below,
PHP Code:
function display_children($parent, $level) { $result = mysql_query("SELECT a.id, a.label, a.link, Deriv1.Count FROM `menu` a LEFT OUTER JOIN (SELECT parent, COUNT(*) AS Count FROM `menu` GROUP BY parent) Deriv1 ON a.id = Deriv1.parent WHERE a.parent=" . $parent);
$count = 0;
echo '<div>';
echo "<ul id='menu'>";
while (false !== ($row = mysql_fetch_array($result))) { if ($row['Count'] > 0) {
echo "<li><a href='" . $row['link'] . "'>" . $row['label'] . "</a>";
display_children($row['id'], $level + 1);
echo "</li>";
} elseif ($row['Count']==0) {
echo "<li><a href='" . $row['link'] . "'>" . $row['label'] . "</a></li>";
} else;
}
++$count; if (($count % 5) == 0) {
echo "</ul>"; echo "</div><div>";
}
}
display_children(0, 1);
Please help can't work it out, 
|

January 10th, 2013, 07:25 AM
|
|
|
|
Please edit your post and enclose your code in [ PHP ] tags. See the sticky at the top of this forum.
__________________
There are 10 kinds of people in the world. Those that understand binary and those that don't.
|

January 10th, 2013, 07:52 AM
|
|
|
|
1) Don't use the deprecated mysql extensions. Switch to PDO.
2) You don't say specifically what the problem is but I can see that you end </ul>, end </div> and start <div> but you are not starting <ul> again.
|

January 10th, 2013, 08:28 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 5
Time spent in forums: 2 h 21 m 10 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by gw1500se 1) Don't use the deprecated mysql extensions. Switch to PDO.
2) You don't say specifically what the problem is but I can see that you end </ul>, end </div> and start <div> but you are not starting <ul> again. |
I'm trying to achieve having say 5 records returned for every <Div>
|

January 10th, 2013, 08:36 AM
|
|
|
|
So what are you getting and what is wrong?
|

January 10th, 2013, 09:00 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 5
Time spent in forums: 2 h 21 m 10 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by gw1500se So what are you getting and what is wrong? |
Each div is a page in a jquery flip book. I want to use a database driven menu system for the index pages, in the flip book.
this basic code works and returns 2 records per div
PHP Code:
$con = mysql_connect("localhost","user","pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_tbl", $con);
$result = mysql_query("SELECT * FROM poop");
$count = 0;
echo '<div style="border: thin solid #333;">';
while (false !== ($row = mysql_fetch_array($result)))
{
//echo '<div style="border: thin solid #333;">';
echo $row['id'] . " " . $row['text'];
?>
<a href="<?php echo $row['link']; ?>"><?php echo $row['text']; ?></a>
<?php
echo "<br />";
++$count;
if (($count % 2) == 0) {
echo '</div><div style="border: thin solid #333;">';
}
}
mysql_close($con);
but when I add the menu it the divs get broken ( current attempt)
PHP Code:
function display_children($parent, $level) {
$result = mysql_query("SELECT a.id, a.label, a.link, Deriv1.Count FROM `menu` a LEFT OUTER JOIN (SELECT parent, COUNT(*) AS Count FROM `menu` GROUP BY parent) Deriv1 ON a.id = Deriv1.parent WHERE a.parent=" . $parent);
$count = 0;
$count_b = 0;
++$count_b;
if (($count_b % 1) == 1) {
echo "<div>";
}
while (false !== ($row = mysql_fetch_array($result))) {
echo "<ul>";
if ($row['Count'] > 0) {
echo "<li><a href='" . $row['link'] . "'>" . $row['label'] . "</a>";
display_children($row['id'], $level + 1);
echo "</li>";
} elseif ($row['Count']==0) {
echo "<li><a href='" . $row['link'] . "'>" . $row['label'] . "</a></li>";
} else;
echo "</ul>";
}
++$count;
if (($count % 2) == 0) {
echo "</div>";
}
}
display_children(0, 1);
mysql_close($con);
returns 5 pages, first has 1 record returned, page 2 has 5 records returned and the rest one each.
|

January 10th, 2013, 09:10 AM
|
|
|
|
Using recursive routines can be tricky. It appears that you are generating divs within divs. Is that what you want? What does the resulting HTML source look like?
|

January 10th, 2013, 09:18 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 5
Time spent in forums: 2 h 21 m 10 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by gw1500se Using recursive routines can be tricky. It appears that you are generating divs within divs. Is that what you want? What does the resulting HTML source look like? |
HTML source is
<ul><li><a href='#home'>Home</a></li></ul><ul><li><a href='#code'>Code</a><ul><li><a href='#php'>PHP</a></li></ul><ul><li><a href='#css'>Css</a></li></ul><ul><li><a href='#scripts'>Scripts</a></li></ul><ul><li><a href='#help'>Help</a></li></ul></li></ul><ul><li><a href='#contact'>Contact</a></li></ul><ul><li><a href='#archive'>Archive</a></li></ul><ul><li><a href='#snippet'>Snippet</a></li></ul>
|

January 10th, 2013, 09:43 AM
|
|
|
|
No div's are being output at all. Obviously the problem is your 'if' blocks that are intended to generate them. Your 'count' variables are pretty much useless. You can eliminate them, including the 'if' blocks. They will always be 1. Its my guess that you really don't want to reinitialize them to 0 each time the function is called. You probably want to initialize them to 0 outside the function and then pass them to each call to be incremented and tested.
|

January 10th, 2013, 11:49 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 5
Time spent in forums: 2 h 21 m 10 sec
Reputation Power: 0
|
|
|
Using recursive routines can be tricky, your so right. I think I'll try a different approach, and display the content in a different manor.
Thanks for helping, have learned a lot from you, will help others in this forum if I can.
Kevin.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|