Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8967089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:10:11+00:00 2026-06-15T17:10:11+00:00

I have a PHP application that takes a CSV in input; in the first

  • 0

I have a PHP application that takes a CSV in input; in the first column I have both category and subcategory splitted by a ‘|’. I have to put this stuff in a Open Cart DB, which has the same table for Category and Subcategory (a column “parent_id” indicates the category_id of the Category).
I thought to build a class with all the fields needed as follows:

class Categorie {
    public $category_id;
    public $parent_id;
    public $image;
    public $top;
    public $column;
    public $sort_order;
    public $status;
    public $date_modified;
    public $date_added;
    public $language_id;
    public $name;
    public $description;
    public $meta_description;
    public $meta_keywords;
}

Then I analyze the data:

$categorie = array();
while ( ( $riga_file = fgetcsv( $file, 100000, "\t" ) ) !== false )
{
     $array_el = count( $riga_file );
     for( $el_cur = 0; $el_cur < $array_el; $el_cur++ )
     {
         switch ( $el_cur )
         {
             case 0:
             $colonne = explode( "|", $riga_file[$el_corr] );
             $categoria = new Categorie();
             $categoria->image = "";
             $categoria->top = 1;
             $categoria->column = 1;
             // ... bla adding description data
             if( $categorie[$colonne[0]] == NULL ) // (1)
             {
                 $categoria->name = $colonne[0];
                 $categoria->category_id = $id_categoria;
                 $categoria->parent_id = 0;
                 $categorie[$colonne[0]] = $categoria;
                 $id_categoria++;
             }
             if( $colonne[1] != NULL ) // (2)
             {
                 $categoria->name = $colonne[1];
                 if( $categorie[$colonne[1]] == NULL )
                 {
                     $categoria->category_id = $id_categoria;
                     $categoria->parent_id = $categorie[$colonne[0]]->category_id;
                     $categorie[$colonne[1]] = $categorie;
                     $id_categoria++;
                 }
             }
             break;
         }   

I should have an array filled with a collection of unique objects like:

  // OUT(3)
  Category 1
  Subcategory 1
  Category 2
  Subcategory 2...

If I put echoes of $categoria (not $categorie) inside (1) and (2), I see exactly what I wrote down in OUT(3), which leads me to think that my “engine” is correct. The problems come when I try to search into the array:

echo "<table border='1'>";
            foreach( $categorie as $value )
            {
                echo "<tr>
                           <td>$value->category_id</td>
                           <td>$value->parent_id</td>
                           <td>$value->name</td>
                      </tr>";
            }
            echo "</table>";

because I don’t get Categories at all, and most but not all Subcategories.
Am I doing something wrong with “search engine” or I misunderstood something with PHP Arrays (probably because I come from C++ and this php is f**king my mind)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T17:10:12+00:00Added an answer on June 15, 2026 at 5:10 pm

    I solved the problem, or better: problems.

    1) I mispelled a variable: in (2) I wrote:

    $categorie[$colonne[1]] = $categorie;
    

    The correct writing is:

    $categorie[$colonne[1]] = $categoria;
    

    2) The bigger problem was from my “unknowing” of PHP, in fact, fixed the problem 1, I got an output like:

    subcategory1
    subcategory1
    subcategory2
    subcategory2
    

    and so on. This lead me to guess that when I do:

    $categorie[$colonne[1]] = $categoria;
    

    PHP doesn’t copy the values inside, but uses addresses instead. Follows the right code:

    switch ( $el_corr )
                        {
                            case 0:                                                 // Categorie e sottocategorie
                                $colonne = explode( "|", $riga_file[$el_corr] );
                                $categoria = new Categorie();
                                $categoria->image = "";
                                $categoria->top = 1;
                                $categoria->column = 1;
                                $categoria->sort_order = 0;
                                $categoria->status = 1;
                                // Category_Description Campi
                                $categoria->language_id = 1;
                                $categoria->name = $colonne[0];
                                $categoria->description = "";
                                $categoria->meta_description = "";
                                $categoria->meta_keywords = "";
                                ////////////////////////
                                if( $categorie[$colonne[0]] == NULL )
                                //if( !trova_categorie( $colonne[0], $categorie ) )
                                {
                                    $categoria->name = $colonne[0];
                                    $categoria->category_id = $id_categoria;
                                    $categoria->parent_id = 0;
                                    $categorie[$colonne[0]] = $categoria;
                                    $id_categoria++;
                                }
                                $categoria = new Categorie();
                                $categoria->image = "";
                                $categoria->top = 1;
                                $categoria->column = 1;
                                $categoria->sort_order = 0;
                                $categoria->status = 1;
                                // Category_Description Campi
                                $categoria->language_id = 1;
                                $categoria->name = $colonne[0];
                                $categoria->description = "";
                                $categoria->meta_description = "";
                                $categoria->meta_keywords = "";
                                if( $colonne[1] != NULL )
                                {
                                    $categoria->name = $colonne[1];
                                    if( $categorie[$colonne[1]] == NULL )
                                    //if( !trova_categorie( $colonne[1], $categorie ) )
                                    {
                                        $categoria->category_id = $id_categoria;
                                        $categoria->parent_id = $categorie[$colonne[0]]->category_id;
                                        $categorie[$colonne[1]] = $categoria;
                                        $id_categoria++;
                                    }
                                }
                                break;
                        } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a PHP/MYSQL application that collects user input, including special characters like ø,ü,ñ,
I have a PHP based application that I need to work on both a
I have an application I wrote in PHP (on symfony) that imports large CSV
I have a php application that uses the google feed api to get the
I have a PHP application that I have been having some problems with, some
I have a php application that is installed on several servers and all of
I have a PHP application that sends email using the pear Mail function. Unfortunately
I have a PHP application that is built around the MVC architecture without any
I have a php web application that uses big cookies to store a lot
I have written a PHP application that uses Objects heavily. Added, deleting, updating etc..

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.