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 8610427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:07:35+00:00 2026-06-12T04:07:35+00:00

Now I have create categories and create post is successful but when I edit

  • 0

Now I have create categories and create post is successful but when I edit my post I have problem if edit my post I will lose my category .. I need to get my category in table and I can change it

<?php

$sql = "SELECT catid, catname, parentid FROM categories";
$res = mysql_query($sql);

// initialize $categories to make sure it is an array
$categories = array();
while ($row = mysql_fetch_assoc($res)) {
$parent = intval($row['parentid']);
$categories[$parent][] = $row;
    }
    ?>
    <table border="0" cellpadding="10" cellspacing="0">
    <tr>
    <td valign="top">

    <?php
    $category_string = "";
    function build_categories_options($parent, $categories, $level) {
        global $category_string;
        if (isset($categories[$parent]) && count($categories[$parent])) {
            $level .= " - ";
            foreach ($categories[$parent] as $category) {
                $opt_value = substr($level.$category['catname'],3);
                $category_string .= '<option value=""></option><option value="'.$category['catid'].'">'.$opt_value.'</option>';
                build_categories_options($category['catid'], $categories, $level);
            }
            $level = substr($level, -3);
        }
        return $category_string;
    }
    $category_options = build_categories_options(0, $categories, '');
    $category_options = '<select class="chosen" name="categories" id="categories">'.$category_options.'</select>';
    echo $category_options; 
    ?>
</td>

My problem located in line 25

$category_string .= '<option value=""></option><option value="'.$category['catid'].'">'.$opt_value.'</option>';

I need to get my category in first and show the rest of the results.

  • 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-12T04:07:37+00:00Added an answer on June 12, 2026 at 4:07 am

    For each category, you are displaying two options, an empty option and one with the category information:

    $category_string .= '<option value=""></option><option value="'.$category['catid'].'">'.$opt_value.'</option>';
    

    This is inside your loop. So every time your loop iterates, two options will be created. A blank one and one with your category. I’m betting you just need one blank option at the very beginning of the <select>. I think this is what you wanted:

    // notice we are initializing $category_string with an empty option here
    $category_string = '<option value=""></option>';
    
    function build_categories_options($parent, $categories, $level) {
        global $category_string;
        if (isset($categories[$parent]) && count($categories[$parent])) {
            $level .= " - ";
            foreach ($categories[$parent] as $category) {
                $opt_value = substr($level.$category['catname'],3);
    
                // removed extra empty category and put it in $category_string initialization
                $category_string .= '<option value="'.$category['catid'].'">'.$opt_value.'</option>';
                build_categories_options($category['catid'], $categories, $level);
            }
            $level = substr($level, -3);
        }
        return $category_string;
    }
    

    Also, as @MoeTsao mentioned in the comments, try to avoid using mysql_* functions, as their use is discouraged by PHP. Instead, use mysqli_* or PDO.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently working in this problem for hours now. I have to create
I'm trying to get all the categories about the specific blogpost but it will
I have create my own NSOpenGLView class, right now the data that i want
I've been useing VS2005 to create sql reports and now have to upgrade to
So right now I have a simple canvas element with functions that create random
I'm trying to create a faster query, right now i have large databases. My
I have been trying for almost a week now to create an SQLite database
I have created a TFS build definition to create our software releases. Now I
i want create image animation , i have 50 images with png format now
I have a big div with a big background-image. Now I want to create

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.