Current location: Hot Scripts Forums » Programming Languages » PHP » Please help me sort out a small part of my php script...


Please help me sort out a small part of my php script...

Reply
  #1 (permalink)  
Old 02-26-04, 06:55 PM
robertisunemployed robertisunemployed is offline
Newbie Coder
 
Join Date: Feb 2004
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Please help me sort out a small part of my php script...

Hi there,

I was hoping someone could help me out with a problem I've been having with a part of my classifieds script on my site.

This part in question creates Yahoo-style tables of all the catagory and sub-catagory listings on the main page of my site.

The problem (hopefully simple!) is this the script generates the titles such as this:

Catagory,
Subcatagory, Subcatagory, Subcatagory etc.

whereas I really want it to look like this:

Catagory,
Subcatagory,
Subcatagory,
Subcatagory etc.

My knowledge of php and indeed the css. style sheet which formats the page is limited, but I thought that the bit of script I have included probably is responsible.

I'd really appreciate it if some of you would have a look and tell me how I can change this, it's rather ruining my whole site development at the moment!

Anyway thanks in advance,
Robert

Here's the script:

// ================================================== ==========
// generates the Yahoo style table with all first and second
// level categories with number of listings and number of new
// listings
// ================================================== ==========
//
function generate_home_category_list($portal) {

global $BBClass_verb;
$now = date("Y-m-d H:i:s");

$query = "SELECT
classifieds_categories.*,
COUNT(classifieds_listings.listing_id) AS item_count
FROM
classifieds_categories
LEFT JOIN
classifieds_listings ON
((classifieds_categories.category_id=SUBSTRING(cla ssifieds_listings.category_id,1,2) OR
classifieds_categories.category_id=SUBSTRING(class ifieds_listings.category_id,1,4)) AND
classifieds_listings.status=20 AND classifieds_listings.exp_date >= '$now')
WHERE
(classifieds_categories.display=1 AND
(classifieds_categories.category_id LIKE '__' OR classifieds_categories.category_id LIKE '____'))
GROUP BY
classifieds_categories.category_id
ORDER BY
classifieds_categories.category_name, classifieds_categories.category_id";
$this->db->query($query);

if ($this->db->num_rows() > 0) {

$output = '<table border="0" cellspacing="0" cellpadding="10" width="100%"><tr>' . "\n";

// fetch the data
while ($this->db->next_record()) {
$category_id = $this->db->f('category_id');
$category_name = $this->db->f('category_name');
$item_count = $this->db->f('item_count');
$parent = substr($category_id,0,2);
$is_parent = (strlen($category_id) == 2) ? 1 : 0;
if ($is_parent) {
$parent_categories[$category_id] = array( "category_name" => $category_name,
"item_count" => $item_count);
} else {
$child_categories[$parent][$category_id] = array( "category_name" => $category_name,
"item_count" => $item_count);
}
}
// build the content
$i=0;
reset($parent_categories);
while(list($key, $val) = each($parent_categories)) {

$output .= '<td align="left" valign="top" width="50%"><span class="cfmaincat"><b>' . "\n";
$output .= '<a class="cfmaincat" href="'.$portal.'?cf_action=list&category_id='.$ke y.'">'.$val["category_name"].'</a>';
if ($val["item_count"] > 0 || $this->show_zero_category_count) {
$output .= '&nbsp;('.$val["item_count"].')';
}
$output .= '</b></span>';

if ($this->show_children_at_home) {

$output .= '<br /><span class="cfsubcat">' . "\n";
$this_child = $child_categories[$key];
reset($this_child);
$j=0;
while(list($key, $val) = each($this_child)) {
if ($j > 0) $output .= ', ';
$output .= '<a class="cfsubcat" href="'.$portal.'?cf_action=list&category_id='.$ke y.'">' . $val["category_name"] . '</a>';
if ($val["item_count"] > 0) {
$output .= '&nbsp;('.$val["item_count"].')';
}
$j++;
}
}

$output .= '</td>';
if ($i % 2) {
$output .= '</tr><tr>' . "\n";
$add_td = false;
} else {
$add_td = true;
}
$i++;


}
if ($add_td) $output .= "<td>&nbsp;</td>";
$output .= '</tr></table>';

} else {
$output = '<p class="bbclasserror">There are no categories defined yet.</p>';
}

return $output;
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 02-27-04, 01:41 AM
blaw's Avatar
blaw blaw is offline
Junior Code Guru
 
Join Date: Dec 2003
Location: Vancouver, BC, Canada
Posts: 550
Thanks: 0
Thanked 0 Times in 0 Posts
Hello,

It's an HTML issue. You need a line break after each subcategory. I believe this is the comma that is placed after each subcategory:

PHP Code:

if ($j 0$output .= ', '
So this is a good place to insert a line break like this:

PHP Code:

if ($j 0$output .= ',<br />'
Hope this helps.
__________________
Blavv =|
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 02-27-04, 09:00 PM
robertisunemployed robertisunemployed is offline
Newbie Coder
 
Join Date: Feb 2004
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks so much blaw,

You were right and I've managed to fix this problem.
I really appreciate your help on the subject.

However as I ad more catagorys and subcatagories to my site I've encountered another problem. Once again I think its based on the above part of the script.

Basically the script is only allowing 2 columns of catagories to be printed on the page (leaving a large empty gap on most of the right hand side of the screen) when I need there to be three or more. Is there a simple way of changing the number of columns?

I've trawled through the above script, but like I said already, I am a bit of a novice so don't know exactly what to look for.

I hope someone can help....

Thanks again Blaw, it's down to very helpful individuals such as yourself that I'm managing to get anything done at all!

Robert
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 02-29-04, 02:14 PM
blaw's Avatar
blaw blaw is offline
Junior Code Guru
 
Join Date: Dec 2003
Location: Vancouver, BC, Canada
Posts: 550
Thanks: 0
Thanked 0 Times in 0 Posts
Hi again,

Quote:
Originally Posted by robertisunemployed
However as I ad more catagorys and subcatagories to my site I've encountered another problem. Once again I think its based on the above part of the script.

Basically the script is only allowing 2 columns of catagories to be printed on the page (leaving a large empty gap on most of the right hand side of the screen) when I need there to be three or more. Is there a simple way of changing the number of columns?
Excuse me, but I'm a bit confused now. I thought you wanted only one category/subcategory per row like this:

Code:
Catagory1,
Subcatagory1.1,
Subcatagory1.2,
Subcatagory1.3,
Category2,
Subcatagory2.1,
Subcatagory2.2,
Subcatagory2.3,
.
.
.
What exactly are you trying to achieve?
If I can see what you want, I may be able to help.

P.S. If you are sharing(/showing) your code to someone else, you should perhaps use indentation like this for easier reading in general:

PHP Code:

if ($foo == $bar) {

    echo 
'foo_bar';
    if (
$foo == $hogehoge) {
        echo 
'and hoge';
    }
}
else {
    echo 
'foo';

Some people use this instead, too:

PHP Code:

if ($foo == $bar)

{
    echo 
'foo_bar';
    if (
$foo == $hogehoge)
    {
        echo 
'and hoge';
    }
}
else
{
    echo 
'foo';

Either one is much easier to read than this:

PHP Code:

if ($foo == $bar)

{
echo 
'foo_bar';
if (
$foo == $hogehoge)
{
echo 
'and hoge';
}
}
else
{
echo 
'foo';

As for source highlighting, you can use [ php ]your_code[ /php ] on this board (without a white space between the word and brackets)

HTH.
__________________
Blavv =|
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 02-29-04, 05:31 PM
robertisunemployed robertisunemployed is offline
Newbie Coder
 
Join Date: Feb 2004
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
You can have a look at the site here:
http://eveythingfree.buildtolearn.net/classifieds

Disregard the silly catagory names, I was just trying to fill up as much space as possible! As you can see the script never generates more than two columns (on the left hand side) leaving too much space on the right.

Maybe this will make it clearer what I'm trying to do.

Thanks, Robert

Hopefully the script shall be clearer now:


[php]// ================================================== ==========
// generates the Yahoo style table with all first and second
// level categories with number of listings and number of new
// listings
// ================================================== ==========
//
function generate_home_category_list($portal) {

global $BBClass_verb;
$now = date("Y-m-d H:i:s");

$query = "SELECT
classifieds_categories.*,
COUNT(classifieds_listings.listing_id) AS item_count
FROM
classifieds_categories
LEFT JOIN
classifieds_listings ON
((classifieds_categories.category_id=SUBSTRING(cla ssifieds_listings.category_id,1,2) OR
classifieds_categories.category_id=SUBSTRING(class ifieds_listings.category_id,1,4)) AND
classifieds_listings.status=20 AND classifieds_listings.exp_date >= '$now')
WHERE
(classifieds_categories.display=1 AND
(classifieds_categories.category_id LIKE '__' OR classifieds_categories.category_id LIKE '____'))
GROUP BY
classifieds_categories.category_id
ORDER BY
classifieds_categories.category_name, classifieds_categories.category_id";
$this->db->query($query);

if ($this->db->num_rows() > 0) {

$output = '<table border="0" cellspacing="0" cellpadding="10" width="100%"><tr>' . "\n";

// fetch the data
while ($this->db->next_record()) {
$category_id = $this->db->f('category_id');
$category_name = $this->db->f('category_name');
$item_count = $this->db->f('item_count');
$parent = substr($category_id,0,2);
$is_parent = (strlen($category_id) == 2) ? 1 : 0;
if ($is_parent) {
$parent_categories[$category_id] = array( "category_name" => $category_name,
"item_count" => $item_count);
} else {
$child_categories[$parent][$category_id] = array( "category_name" => $category_name,
"item_count" => $item_count);
}
}
// build the content
$i=0;
reset($parent_categories);
while(list($key, $val) = each($parent_categories)) {

$output .= '<td align="left" valign="top" width="50%"><span class="cfmaincat"><b>' . "\n";
$output .= '<a class="cfmaincat" href="'.$portal.'?cf_action=list&category_id='.$ke y.'">'.$val["category_name"].'</a>';
if ($val["item_count"] > 0 || $this->show_zero_category_count) {
$output .= '&nbsp;('.$val["item_count"].')';
}
$output .= '</b></span>';

if ($this->show_children_at_home) {

$output .= '<br /><span class="cfsubcat">' . "\n";
$this_child = $child_categories[$key];
reset($this_child);
$j=0;
while(list($key, $val) = each($this_child)) {
if ($j > 0) $output .= ', ';
$output .= '<a class="cfsubcat" href="'.$portal.'?cf_action=list&category_id='.$ke y.'">' . $val["category_name"] . '</a>';
if ($val["item_count"] > 0) {
$output .= '&nbsp;('.$val["item_count"].')';
}
$j++;
}
}

$output .= '</td>';
if ($i % 2) {
$output .= '</tr><tr>' . "\n";
$add_td = false;
} else {
$add_td = true;
}
$i++;


}
if ($add_td) $output .= "<td>&nbsp;</td>";
$output .= '</tr></table>';

} else {
$output = '<p class="bbclasserror">There are no categories defined yet.</p>';
}

return $output;
}[\php]
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 02-29-04, 05:35 PM
robertisunemployed robertisunemployed is offline
Newbie Coder
 
Join Date: Feb 2004
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Sorry I still can't post that script the right way!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 03-01-04, 05:31 PM
blaw's Avatar
blaw blaw is offline
Junior Code Guru
 
Join Date: Dec 2003
Location: Vancouver, BC, Canada
Posts: 550
Thanks: 0
Thanked 0 Times in 0 Posts
Hello, the link you posted, it gave me 404 error (not found). Was it the correct URL or is it my browser/connection...?
__________________
Blavv =|
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 03-03-04, 07:08 PM
robertisunemployed robertisunemployed is offline
Newbie Coder
 
Join Date: Feb 2004
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Smile

Try this:

http://eveythingfree.buildtolearn.ne...ieds/index.php

It should work. Or at least it does on my computer.

And I still have not sorted this out so I'd really appreciate any help...


Thanks,
Robert

Last edited by robertisunemployed; 03-03-04 at 07:10 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #9 (permalink)  
Old 03-03-04, 11:02 PM
Infinite_Hackers's Avatar
Infinite_Hackers Infinite_Hackers is offline
Coding Addict
 
Join Date: Dec 2003
Posts: 307
Thanks: 0
Thanked 0 Times in 0 Posts
Maby this might help
http://www.compsci.ca/v2/viewtopic.php?t=2581

it's a simular problem, and the solution is much better, and makes the code smaller.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there any integrity of script rankings? webmaster@atmanager.com Hot Scripts Forum Questions, Suggestions and Feedback 17 08-06-04 01:12 AM
Using crond to run a php script with redirect and refresh php_newbie PHP 2 02-09-04 01:03 PM
Looking for php script whl626 Script Requests 3 01-31-04 05:48 PM
100 Web Templates & 10 PHP Scripts for sale! HostersUK.co.uk General Advertisements 0 01-10-04 01:31 AM
Affiliate script (PHP) whtiebear Job Offers & Assistance 2 12-21-03 01:12 AM


All times are GMT -5. The time now is 01:38 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.