Skip to main content
zohrapardesi
Participating Frequently
November 8, 2017
Answered

Sub Cat List get vanish when click on a sub cat name

  • November 8, 2017
  • 2 replies
  • 1730 views

Hi Guys, if anybody there to help me out in the issue below please:

I am learning PHP/PDO via project in youtube. by learning project I have created code below which is all fine but as soon as I click on a sub category, it shows products into center column but sub category list get vanish, I don't know what mistake I have done, I tried everything to redirect it to its own location i.e. into the left column but remain unsuccessful. it will be appreciated if someone help me in resolving the issue. code is here below:

function for sub cat list:

     function list_all_subcat(){

include("includes/dbconn.php");

if(isset($_GET['cat_id'])){

$cat_id=$_GET['cat_id'];

$subcat_list=$conn->prepare("select * from sub_cat where cat_id='$cat_id'");

$subcat_list->setFetchMode(PDO:: FETCH_ASSOC);

$subcat_list->execute();

while($row_subcat_list=$subcat_list->fetch()):

echo "<li><a href='cat-detail.php?subcat_id=".$row_subcat_list['sub_cat_id']."'>".$row_subcat_list['sub_cat_name']."</a></li>";

endwhile;

}

}   

function called into left column:

<div id="bodyleft">

<div id="leftup">

<h2>Categories</h2>

        <ul>

<?php echo list_all_cat(); ?>

        </ul>

</div>

<div id="leftdown">

      <h2>Sub Categories</h2>

        <ul>

            <?php echo list_all_subcat(); ?>

        </ul>

</div>

</div>

cat-detail.php cpntains below code in body:

<div id="bodycontainer">

    <?php

include('includes/bodyleft.php');

include('includes/bodycenter.php');

include('includes/bodyright.php');

?>

</div>

bodycenter contains below code in body:

<div>

    <ul><?php echo cat_detail();?></ul><br clear='all'> //this will show details into center

    <ul><?php echo subcat_detail();?></ul><br clear='all'>

</div>

Everything is working fine When I click on a Category in left column up, category list remain there and shows the related Sub Category which is OK

But when I click on a Sub Category into sub cat List then everything works fine except sub cat list which go went gone and I again need to click on category. I want this list remain until i click on another category into the category list above in left column Up.

I remain failed to resolve this issue if anybody can help in this I will be thankful to him/her please.

    This topic has been closed for replies.
    Correct answer osgood_

    I don't really understand the question and neither does anyone else judging by the feeback so far. You need to provide a url to the page where this is happening  - but at a guess what is happening is the page is reloading to cat-details.php and when it does that the sub category will not be open until you click on the top level category again.

    <li><a href='cat-detail.php?subcat_id=".$row_subcat_list['sub_cat_id']."'>".$row_subcat_list['su b_cat_name']."</a></li>

    You need to use Ajax in combination with the php to process the information from the database and pass it back to the centre column then your sub-categories will remain visible because when you use Ajax the page does NOT reload.

    2 replies

    zohrapardesi
    Participating Frequently
    November 9, 2017

    Dear osgood, Thanks for your time, I am thankful to you.

    your guess is 100% right, thats what I was asking, and thanks for the suggestion regarding AJAX; although, I am little scared as I started learning PHP then it came to know I need to learn PDO, I have now started learning PDO along with PHP and now AJAX, I wish and Pray someday I be able to make my dynamic website.....Aahhhh looks difficult but well,,,, its not impossible, Thanks Sir

    B i r n o u
    Legend
    November 9, 2017

    well I canno't be formal on this one, but depending on the type of app that you need to develop, you could only focus on JavaScript and develop your app only (at least on major part of it) on client side, then only rely using PHP/PDO to read write your datas... (either if you could also rely on a pure JavaScript server side solution too)... but that is also true that most of the average remote mutual hosting web server propose in most case SQL database.

    so that to say, that AJAX is only a client side asynchrone request... in short, when you send a classic http request, the client hang until the server send any feedback, and then reload the entire page... (the reason that you loose your sub list focus)... on the opposite, a XMLhttRequest (the http request send by 'AJAX') is a asynchrone request type, that do mean, that once the resquest is send... the client continue to live his own life, waiting to catch up the feedback on the fly and can integrate it as he likes on the DOM structure (without having to reload the page)

    now if you mix and merge AJAX technics with all the new JavaScript Client Sode API (from HTML5) ... have some look at and/or HTML 5 JavaScript API Index and/or HTML5 Landscape Overview - dretblog  you'll see that everything to develop an app is here under the use of JavaScript. You have a lot of material out there to learn it... look for SPA (stand for Single Page web Application)

    I agree now that it makes a long road to learn all that before mastering it... but blieve it or not, it is easiest than it seams... and JavaScript is a real simple and intutive language.

    I don't know if we can do that on this forum, but I can orient you on some author that help me to kick over this language...

    pziecina
    Legend
    November 9, 2017

    Hi Birnou,

    It is o/k to recommend courses, books, tutorials, videos, in forum answers, the only stipulation is that the recommendations must be relevant to the question asked.

    osgood_Correct answer
    Legend
    November 8, 2017

    I don't really understand the question and neither does anyone else judging by the feeback so far. You need to provide a url to the page where this is happening  - but at a guess what is happening is the page is reloading to cat-details.php and when it does that the sub category will not be open until you click on the top level category again.

    <li><a href='cat-detail.php?subcat_id=".$row_subcat_list['sub_cat_id']."'>".$row_subcat_list['su b_cat_name']."</a></li>

    You need to use Ajax in combination with the php to process the information from the database and pass it back to the centre column then your sub-categories will remain visible because when you use Ajax the page does NOT reload.

    B i r n o u
    Legend
    November 9, 2017

    yes you're right OS...