Hierarchical menu – Repeated child/grandchild names

  • elrayyes
  • Born
  • Born
  • elrayyes
  • Posts: 2

Post 3+ Months Ago

Hi all. Please understand that I am novice in regards to php/mysql programming.
I have a question regarding building a menu system that will deal with repeated grandchild names under different child names.
The data is imported from a text (tab-delimeted) file to create the table using the following code - very straight forward.

PHP Code: [ Select ]
include "connect.php";
      mysql_query('TRUNCATE TABLE tester;');
      $handle = fopen("$filename", "r");
      while (($data = fgetcsv($handle, 4096, "\t")) !== FALSE)
         $import="INSERT into tester(ProductID,ShortDescription,Description,FamilyID,Cat1,Cat2,Cat3,Cat4) values('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]','$data[7]')";
         mysql_query($import) or die(mysql_error());
      print "Import done";
      print "<form action='import.php' method='post'>";
      print "Type file name to import:<br>";
      print "<input type='text' name='filename' size='20'><br>";
      print "<input type='submit' name='submit' value='submit'></form>";
  1. <?php
  2. include "connect.php";
  4. if(isset($_POST['submit']))
  5.    {
  6.       mysql_query('TRUNCATE TABLE tester;');
  7.       $filename=$_POST['filename'];
  8.       $handle = fopen("$filename", "r");
  9.       while (($data = fgetcsv($handle, 4096, "\t")) !== FALSE)
  10.       {
  11.          $import="INSERT into tester(ProductID,ShortDescription,Description,FamilyID,Cat1,Cat2,Cat3,Cat4) values('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]','$data[7]')";
  12.          mysql_query($import) or die(mysql_error());
  13.       }
  14.       fclose($handle);
  15.       print "Import done";
  16.    }
  17.    else
  18.    {
  19.       print "<form action='import.php' method='post'>";
  20.       print "Type file name to import:<br>";
  21.       print "<input type='text' name='filename' size='20'><br>";
  22.       print "<input type='submit' name='submit' value='submit'></form>";
  23.    }
  24. ?>

Cat1 through Cat4 defines the category levels (i.e. name of the category under each level). Here is an example of the required hierachy of the menu

* Lighting
- Recessed
-- Fluorescent
--- 3-in
--- 4-in
--- 5-in
-- Metal Halide
--- 3-in
--- 4-in
--- 5-in
- Undercabinet
* Lamps
- Fluorescent
- Halogen

You notice that the grandchild naming (i.e. 3-in, 4-in, etc.) is repeated under two different child names (i.e. Fluorescent, Metal Halide). This would cause a problem in showing all the products that fall in different grandchild levels to be shown under the same heading (repeated).
I hope you can see my delima with this. Any help would be very much appreciated.
  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Posts: 6228
  • Loc: South-Africa

Post 3+ Months Ago

A few notes on this:

1. It would be better to combine your queries into one query to take a bit of strain off the MySql server and make this a little faster, for example:

SQL Code: [ Select ]
INSERT INTO `table`(id,name) VALUES(NULL,'R_T'),(NULL,'elrayyes'),(NULL,'Something');

will be faster than
SQL Code: [ Select ]
INSERT INTO `table`(id,name) VALUES(NULL,'R_T');
INSERT INTO `table`(id,name) VALUES(NULL,'elrayyes');
INSERT INTO `table`(id,name) VALUES(NULL,'Something');
  1. INSERT INTO `table`(id,name) VALUES(NULL,'R_T');
  2. INSERT INTO `table`(id,name) VALUES(NULL,'elrayyes');
  3. INSERT INTO `table`(id,name) VALUES(NULL,'Something');

Adjusting your PHP code to build one bigger query and then execute it would be better.

2. echo()ing is faster than print()ing ... This might be by miliseceonds, but if you have lots of places saving miliseconds at a time it might help a little on higher traffic pages.

3. Using single quotes around strings is faster than using double quotes, for example:

PHP Code: [ Select ]
echo 'Hello my name is R_T';

will be faster than
PHP Code: [ Select ]
echo "Hello my name is R_T";

Again, this is a really tiny difference measured in miliseconds, but on frequently accessed pages it might make a little difference. Note though that variables cannot be echo()d in single quoted strings, so the following will NOT work:

PHP Code: [ Select ]
$username = 'R_T';
echo 'Hello my name is $username';
  1. $username = 'R_T';
  2. echo 'Hello my name is $username';

This will output: Hello my name is $username

Whereas the following WILL work:
PHP Code: [ Select ]
$username = 'R_T';
echo "Hello my name is $username";
  1. $username = 'R_T';
  2. echo "Hello my name is $username";

This will output: Hello my name is R_T

4. Now, regarding your actual issue, do you have an example of the file you are importing somewhere so I can see what it looks like?

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 40 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum

© 1998-2017. Ozzu® is a registered trademark of Unmelted, LLC.