I have a Daily Deals website of which I desire to limit the viewing of one of the product catagories to logged in users only. here is the catagory code.

<div class="categories">
<h3><?php echo $array['Categories'];?></h3>
<ul>
    <?php 
        $category_total  = num_rows(mkr_select('ig_category','cat_name,cat_id,cat_image','cat_status=1'));
        $category_select = mkr_select('ig_category','cat_name,cat_id,cat_image','cat_status=1 ORDER BY cat_name ASC  LIMIT 0,3');
        if(num_rows($category_select)>0)
        {
            while($fetch_category = fetch_array($category_select))
            { 
            ?>
        <li><a href="all_deals.php<?php echo $urladdon; ?>cat_id=<?php echo $fetch_category['cat_id']; ?>&cat_name=<?php echo $fetch_category['cat_name']; ?>" <?php if($_GET["cat_id"] == $fetch_category['cat_id']) { ?>class="categoriesactive" <?php } ?>><?php echo strtoupper($fetch_category['cat_name']); ?></a></li>
        <?php
            }
        ?>
        <?php
            $category_select2 = mkr_select('ig_category','cat_name,cat_id,cat_image','cat_status=1 ORDER BY cat_name ASC LIMIT 3,50');
            while($fetch_category2 = fetch_array($category_select2))
            {
        ?>
        <li class="tgl"><a href="all_deals.php<?php echo $urladdon; ?>cat_id=<?php echo $fetch_category2['cat_id']; ?>&cat_name=<?php echo $fetch_category2['cat_name']; ?>" <?php if($_GET["cat_id"] == $fetch_category2['cat_id']) { ?>class="categoriesactive" <?php } ?>><?php echo strtoupper($fetch_category2['cat_name']); ?></a></li>
        <?php
            }
            if($_GET["cat_id"] == "")

            {
                if($category_total>=3)
                {
                ?>
                <li class="more"><a style="cursor:pointer; text-align:right; font-size:12px;" id="viewall_a"><?php echo $array['View']; ?>&nbsp;&gt;&gt;&nbsp;</a></li>
                <?php
                }
                ?>
                <li class="less"><a style="cursor:pointer; text-align:right; font-size:12px;" id="viewall_b"><?php echo $array['View']; ?>&nbsp;&lt;&lt;&nbsp;</a></li>
                <?php
            }
        }
        ?>
</ul>
</div>

Here is the portion I believe that concerns what I am trying to do.

<?php
            $category_select2 = mkr_select('ig_category','cat_name,cat_id,cat_image','cat_status=1 ORDER BY cat_name ASC LIMIT 3,50');
            while($fetch_category2 = fetch_array($category_select2))
            {
        ?>
        <li class="tgl"><a href="all_deals.php<?php echo $urladdon; ?>cat_id=<?php echo $fetch_category2['cat_id']; ?>&cat_name=<?php echo $fetch_category2['cat_name']; ?>" <?php if($_GET["cat_id"] == $fetch_category2['cat_id']) { ?>class="categoriesactive" <?php } ?>><?php echo strtoupper($fetch_category2['cat_name']); ?></a></li>
        <?php
            }
            if($_GET["cat_id"] == "")

I know first I must check for the session if(isset($_SESSION[""])) so if session is set show the requested content "cat_id=4". If session is unset direct viewer to login page. unset($_SESSION["login.php"]);

Am I anywhere close to warm?

Thank you in advance for any of your responses!

Recommended Answers

All 33 Replies

Member Avatar for diafol

Are you close to warm? Well it depends what you want to happen. Personally, I write a lot of 'pseudocode' before I actually get down to hard coding. That way I know which include files I need, which parts may be duplicated and how to mitigate against that etc.

So let us assume that what you want is the following:

IF SESSION IS SET
    RUN QUERY
ELSE
    REDIRECT TO LOGIN PAGE

Firstly, you need to ensure that you place session_start() at the top of every page. I usually just store the user_id (or sometimes the username as well) in a session, so to check for a login, you could do something like the following:

if(isset($_SESSION['id']))
{
    //run query
}else{
    header("Location: login.php");
    exit();
}

Or was there something else you were looking for?

Thank you for your response.

The session start is found in the library.php common file includes. The other session_id checks are on pages served, such as the checkout page and user information page.

The query or catagory fetch is contained in the code above I believe and it is placed in a "right column include file". Viewers may select product catagories and view the contents but I wish to reserve a private catagory for "members only / logged in users". So I am trying to restrict views of "cat_id=4". Rather than require login to view all catagories and to provide them as a preview.

So I only need to check the session on the fetching of catagory 4.

I thank you for the else statement. I was hung up on the if. I also have a prestashop that I stumble around in but there is tons of tutorials for this software. This daily deals software I purchased from a company.

I am well versed in html and css and I wish to learn the programing side of my websites. Are you still interested in guiding me from here?

Thank you for your consideration.

Member Avatar for diafol

No problem.

Your category table could be set up something like this...

cat_id PK
label
description 
cat_type (tinyint[1], default 0: e.g. 0 for public 1 for logged)

SO if an user is loggedin, you could do this...

$where = "WHERE cat_id = $cat_id";
if(!$logged) $where .= " AND cat_type = 0";

Which will return only public cat records for unlogged and all records for logged.

diafol, I am still here. Truth is I have been studying my code all this time but I don't think I'm up to par on my skills. I was able to create the cat_type field but after that I am at a loss. I see the logic in your direction but I don't know what to do with it on my own.

I am inclined to close out this thread and seek some professional tutoring or a seminar. Perhaps you might have guidance for me regarding a wise choice with that as well.
I thank you for your time and attention.

Sincere

Member Avatar for diafol

You don't need to close the thread. We're all here to help. The main difference between this site and something like StackOverflow is that we encourage learning as opposed to just giving out a solution. If you can articulate the issue, we'll do our best to guide you through the process(es).

Yeah I want to learn. That is why I came to Daniweb. However I am interested and seeking a more comprehensive outline for training for the first of the year. I thank you for your support! Here goes;

The code for the unordered list of category selections is above. Also there is the area that dispays the contents of the category selection.

<div class="content_left_bottom_other" style="margin-top:0;">
                <h1 class="h1"><?php if(isset($_GET["cat_id"])) { echo ucwords($_GET["cat_name"]); } elseif(isset($_SESSION["city_search"]) && ($_SESSION["city_search"] != 'all')) { echo $array['Deals in '].ucfirst(str_replace(" ", "&nbsp;", $search_city_name)); } elseif(isset($_GET["city_id"]) == "all") { echo $array['All Deals']; } else { ?><?php echo $array['All Deals'];?><?php } ?></h1>
                <hr class="hr_small" />
                <div class="content_left_bottom_left_description_other"> 
                <?php
                if($deal_num_rows <= 0)
                {
                    $reduce_right = 1;
                    echo "<h3><center>".$array['Sorry, no deals now!']."</center></h3>";
                }
                else
                {
                ?>

So I am thinking,

<?php
{
$where = "WHERE cat_id = $cat_id";
if(!$logged) $where .= " AND cat_type = 0";
}   
    else{
header("Location: login.php");
exit();
}
?>

And I don't know where to put it. In the fetch or the display. Also in the construct I'm thinking there should be a get for the cat_type

if(isset($_GET["cat_type"]))
Member Avatar for diafol

That first bit of code looks far too complicated. I'm busy for about an hour, but I'll be back. :) Anybody else welcome to chip in.

I thought it looked a little jumbled too but I thought it best for it to wait for some advanced study or refining. I will be away for 6hrs.

Thank you.

Member Avatar for diafol

Having trawled through your html/php mash, I got this:

if(isset($_GET["cat_id"])) {
    $h1 = ucwords($_GET["cat_name"]); 
} elseif(isset($_SESSION["city_search"]) && ($_SESSION["city_search"] != 'all')) {
    $h1 = $array['Deals in '].ucfirst(str_replace(" ", "&nbsp;", $search_city_name)); 
} elseif(isset($_GET["city_id"]) && $_GET["city_id"] == "all") { 
    $h1 = $array['All Deals']; //is there a need for this branch as it's covered below?
} else { 
 $h1 = $array['All Deals'];
}

if($deal_num_rows <= 0)
{
    $reduce_right = 1;
    $h3center = $array['Sorry, no deals now!'];
} else {
    //...??....
}

Place that above the <!doctype html>
You can now echo $h1 and $h3center into the relevant places in the html...

<h1 class="h1"><?php echo $h1;?></h1>

So, separation of php and html makes life a lot easier. Also, indenting your code helps as you can now spot unequal braces and brackets a lot easier.
I'm still concerned about the naming of your array items though. ALthough I assume that they are valid, it makes sense to 'slugify' them to take out any spaces...

So $array['Deals in '] should become $array['deals_in'] and $array['All Deals'] should become $array['all_deals']. Capitalization can also lead to confusion, although item names are case-insensitive (I think).

This:

ucwords($_GET["cat_name"]);

Struck me as a bit odd. If you're passing the cat_id in the url quersystring, surely you'll extract associated info like cat_name from the DB? So there's no need to pass that in the url querystring too.

So bearing the html/php bit in mind...

<div class="categories">
<h3><?php echo $array['Categories'];?></h3>
<ul>
    <?php 
        $category_total  = num_rows(mkr_select('ig_category','cat_name,cat_id,cat_image','cat_status=1'));
        $category_select = mkr_select('ig_category','cat_name,cat_id,cat_image','cat_status=1 ORDER BY cat_name ASC  LIMIT 0,3');
        if(num_rows($category_select)>0)
        {
            while($fetch_category = fetch_array($category_select))
            { 
            ?>
        <li><a href="all_deals.php<?php echo $urladdon; ?>cat_id=<?php echo $fetch_category['cat_id']; ?>&cat_name=<?php echo $fetch_category['cat_name']; ?>" <?php if($_GET["cat_id"] == $fetch_category['cat_id']) { ?>class="categoriesactive" <?php } ?>><?php echo strtoupper($fetch_category['cat_name']); ?></a></li>
        <?php

That's just a horrid mess. Again separate the php from the html as much as possible and just drop in the odd echo here and there in to the html body.

Ok I have installed your updated code. Had some little errors with the syntax and how it lines up. anyway it is at a place where it is working.

The $h1 array echos the catagory names when the catagory is selected but does not echo the "All Deals" when on the all deals landing page and no category selected. I am also doing something wrong with the $h3 echo as well because it does not output "sorry no deals now" when the catagory is empty.

 <div class="content_left_bottom_other" style="margin-top:0;">

     <h1><?php echo $h1; ?></h1>

            <hr class="hr_small" />
            <div class="content_left_bottom_left_description_other">             
<?php
        if($deal_num_rows <= 0)
    {
            $reduce_right = 1;

            echo $h3center;
    }

else
    {
?>
                    <div class="alldeals_banner">
                    <table width="100%">
                    <tr>
                        <td width="46%" rowspan="10" valign="top"><a href="index.php?id=<?php echo $deal_fetch["id"]; ?>"><img src="<?php echo $single_deal_image; ?>" alt="image" height="320" width="300"/></a></td>
                        <td width="54%" valign="top"><h1 style="margin:0; padding:0;"><?php echo ucfirst(substr($deal_fetch["deal_name"], 0, 33)); ?></h1></td>
                    </tr>

The else statement continues on to the alldeals_banner. More of a jumble I'm afraid. I'm sure there is a place for it at the top but I couldn't figure it out.

}
    else
{
    ?>
    <div class="alldeals_banner">

I have been looking at this for hours and I don't know what to do next.

Member Avatar for diafol

One little tip too, you've got all that inline styling and formatting attributes - take them out and put them in a CSS file, or the very least a style tag in the head area. You want your html to be as lean as possible.

Separate your php | html | css | js (if used) as much as possible. Syntax error -

<div class="content_left_bottom_other" style="margin-top:0;"

No closing '>' - Could be

<div class="content_left_bottom_other">

Anyway, I've cleaned up the code to something I can understand...

<div class="content_left_bottom_other">
    <h1><?php echo $h1; ?></h1>
    <hr class="hr_small" />
    <div class="content_left_bottom_left_description_other">
    <?php
    if($deal_num_rows <= 0)
    {
        $reduce_right = 1;
        echo $h3center;
    }else{
    ?>
    <div class="alldeals_banner">
        <table>
            <tr>
                <td rowspan="10">
                    <a href="index.php?id=<?php echo $deal_fetch["id"]; ?>"><img src="<?php echo $single_deal_image; ?>" alt="image" height="320" width="300"/></a>
                </td>
                <td>
                    <h1><?php echo ucfirst(substr($deal_fetch["deal_name"], 0, 33)); ?></h1>
                </td>
            </tr>

Notice the indenting.

Front end images

I'm not sure about the div closing I was doing an edit on the post trying to indent it correctly. The closing is in the page file but it may have been missing before as I was doing an edit while you posted.
The whole code is like this and contains almost 5000 files I think.

I thought it one to learn on when I purchased it but it is jumbled up and doesn't really align with the coding tutorials. I think it confuses me more that anything.

If I could find the right mentor / instructor I would pay to learn how to code my site from scratch.

I am however where I am at today. chuckle

Member Avatar for diafol

Heh. So what are the issues? I couldn't really make it out from the images.

The $h1 array echos the catagory names when the catagory is selected but does not echo the "All Deals" when on the all deals landing page and no category selected.

I am also doing something wrong with the $h3 echo as well because it does not output "sorry no deals now" when the catagory is empty.

Member Avatar for diafol

SO I guess we're back to this...

if(isset($_GET["cat_id"])) {
    $h1 = ucwords($_GET["cat_name"]); 
} elseif(isset($_SESSION["city_search"]) && ($_SESSION["city_search"] != 'all')) {
    $h1 = $array['Deals in '].ucfirst(str_replace(" ", "&nbsp;", $search_city_name)); 
} elseif(isset($_GET["city_id"]) && $_GET["city_id"] == "all") { 
    $h1 = $array['All Deals']; //is there a need for this branch as it's covered below?
} else { 
 $h1 = $array['All Deals'];
}

If you add to this...

if(isset($_GET["cat_id"])) {
    $h1 = "CAT NAME" . ucwords($_GET["cat_name"]); 
} elseif(isset($_SESSION["city_search"]) && ($_SESSION["city_search"] != 'all')) {
    $h1 = "DEALS IN" . $array['Deals in '].ucfirst(str_replace(" ", "&nbsp;", $search_city_name)); 
} elseif(isset($_GET["city_id"]) && $_GET["city_id"] == "all") { 
    $h1 = "ALL DEALS 1" . $array['All Deals']; //is there a need for this branch as it's covered below?
} else { 
 $h1 = "ALL DEALS 2" . $array['All Deals'];
}

As a temp measure, at least you'll see which branch is being executed.

BINGO! Heres how it ended up.

    if(isset($_GET["cat_id"])) {
        $h1 = "" . ucwords($_GET["cat_name"]);
    } elseif(isset($_SESSION["city_search"]) && ($_SESSION["city_search"] != 'all')) {
        $h1 = "DEALS IN" . $array['Deals in '].ucfirst(str_replace(" ", "&nbsp;", $search_city_name));
    } elseif(isset($_GET["city_id"]) && $_GET["city_id"] == "all")
    {
    }
        else {
        $h1 = "ALL DEALS" ;
    }
    {
        $h3center = "Sorry, no deals now!" ;
    }

And.

<div class="content_left_bottom_other" style="margin-top:0;">
    <h1><?php echo $h1; ?></h1>
    <hr class="hr_small" />
<div class="content_left_bottom_left_description_other">             <?php
        if($deal_num_rows <= 0)
    {
        $reduce_right = 1;
?>
    <h3><center><?php echo $h3center; ?></center></h3>
<?php
    }

        else
    {
?>
<div class="alldeals_banner">

So from the origional goal here is what I wish to do:

  1. IF SESSION IS NOT SET OR NOT LOGGED IN
  2. SHOW ONLY CAT_TYPE="0"
  3. IF CAT_TYPE="1" IS SELECTED
  4. CHECK FOR SESSION OR LOGIN
  5. IF LOGGED IN SHOW CONTENT
  6. ELSE
  7. REDIRECT TO LOGIN PAGE
Member Avatar for diafol
} elseif(isset($_GET["city_id"]) && $_GET["city_id"] == "all")
{
}
    else {
    $h1 = "ALL DEALS" ;
}
{
    $h3center = "Sorry, no deals now!" ;
}

I don't understand this. You've got something odd going on. I advise using an IDE that can highlight errors on the fly.

} elseif(isset($_GET["city_id"]) && $_GET["city_id"] == "all"){
    //do nothing
} else {
    $h1 = "ALL DEALS" ;
}
//there should be an if statement below or what? Or should the below be the else statement instead of the above?
{
    $h3center = "Sorry, no deals now!" ;
}

Seeing as you've done the pseudocode, that should be pretty simple. However pretty confusing too. Looking at it it just gives me this...

OK, so the cat_id is being passed in the url querystring, you then need to retrieve the cat_type from the DB (public/private) and check to see whether the user is logged in or not

$logged = (isset($_SESSION['user_id'])) ? $_SESSION['user_id'] : 0; 

$cat = fetch_array(...); //your user-defined function 
if($cat['cat_type'] == 0)
{
    $content = show_category($cat); //use a function to avoid code duplication
}else{
    $content = (logged) ? show_category($cat) : 'Please log in to see this category';
}

function showCategory($cat)
{
    $output = ...;
    return output;
}

//further on

<div id="content">
    echo $content;
</div>

The code above makes use of the "ternery operator" - a shorthand form of the if/else conditional structure:

$variable = (if statement without the 'if') ? value-if-true : value-if-false

THought I'd mention that if you've not seen it before. Anyway, that's just a small example, I wouldn't try to place everything into $content as that just mashes up the html/php again.

Internet problems today and will be out of town until Sunday.

okay I'm back and have worked further on this problem.

Your Post 2043374

Indicated that there was still some holes in what I called a final. so I reworked it and came up with this and it seems to work splendidly. I hope you like it better.

      if(isset($_GET["cat_id"])) {
          $h1 = ucwords($_GET["cat_name"]);
    } 
          elseif(isset($_SESSION["city_search"]) && ($_SESSION["city_search"] != 'all')) {
          $h1 = "DEALS IN" . $array['Deals in '].ucfirst(str_replace(" ", "&nbsp;", $search_city_name));
    }     elseif(isset($_GET["city_id"]) && $_GET["city_id"] == "all")
 {
         $h1 = $array['All Deals'];    //put this back in for now 
 }   else {
          $h1 = "ALL DEALS" ;
 }
         $h3center = 'Sorry, no deals now!';

and;

     <h1><?php echo $h1; ?></h1>

     <hr class="hr_small" />
<div class="content_left_bottom_left_description_other">             
   <?php
         if($deal_num_rows <= 0)
    {
         $reduce_right = 1;
  ?>            
     <h3><?php echo $h3center ?></h3>
   <?php
    }
        else{
  ?>

In the same post regarding the cat_type filter it is indicated to define which arrays to be collected and displayed in a div content. Alas my ignorance is showing again.
How do I do this seamlessly with the following table html placed within the "all deals" container div. You might remember the "all deals div comes after the else statement above within the html and this table displays the deal information from the database.

<table >
    <tr>

    <td align="center" class="value"><?php echo $array['Value']; ?></td>
    <td align="center" class="value"><?php echo $array['Discount']; ?></td>

    <td align="center" class="value"><?php echo $array['Deal Amount']; ?></td>

    <td align="center" class="value"><?php echo $array['You Save']; ?></td>
       </tr>
       <tr>
    <td align="center" class="index_amt"><?php echo CURRENCY($deal_fetch["deal_actual_amount"]); ?></td>

    <td align="center" class="index_amt"><?php echo $deal_fetch["deal_percentage"].'%'; ?></td>

    <td align="center" class="index_amt"><?php echo CURRENCY($deal_fetch["deal_deal_amount"]); ?></td>

    <td align="center" class="index_amt"><?php echo CURRENCY($deal_fetch["deal_saving_amount"]); ?></td>

        </tr>    
   </table>

I have been studying this and I'm not getting it.
It seems that the arrays are the actual headings?

<tr>
    <td><?php echo $array['Value']; ?></td>
    <td><?php echo $array['Discount']; ?></td>
    <td><?php echo $array['Deal Amount']; ?></td>
    <td><?php echo $array['You Save']; ?></td>
</tr>

Where data displays from the Database.

<tr>
    <td><?php echo CURRENCY($deal_fetch["deal_actual_amount"]); ?></td>
    <td><?php echo $deal_fetch["deal_percentage"].'%'; ?></td>

    <td><?php echo CURRENCY($deal_fetch["deal_deal_amount"]); ?></td>
    <td><?php echo CURRENCY($deal_fetch["deal_saving_amount"]); ?></td>
</tr> 

Variables:

$alldeals_query = "SELECT * FROM ig_deals WHERE deal_status=1 AND $searchby '$now' BETWEEN deal_start_date_time AND deal_end_date_time ORDER BY RAND()";

    $deal_query = mkr_query($alldeals_query);

    $deal_fetch = fetch_array($deal_query);

I don't know how to put it together.

Are we done here? Did I do something wrong?

Member Avatar for diafol

Nothing I can see. Sorry haven't come back. Busy. Anybody else?

No time crunch, no problem, it had just been a few days.

thank you

Member Avatar for diafol

It's terribly confusing :)
The trouble with leaving something like this for a while is that you have to go back to the first post to try to make sense of it. And that takes a lot of time. I'll have to have a think.

I have been studying this and I'm not getting it.
It seems that the arrays are the actual headings?

I have no idea what this means

Where data displays from the Database.

You've introduced a new function, CURRENCY. I'm confused about all that. What's not working?

Kind Sir, Thank you for your patience with me.

Everything is working fine. What I am trying to do is limit the viewership of the Members Only shopping catagory, cat_id=4 to logged in members only. While still showing the Members Only catagory link. If a person should select (click) the Members only link it should check for login and perform accordingly. The CURRENCY function shows the currency symbol in the displayed data.

When the Membership Catagory is clicked
check for login
If user is logged in then proceed and show content
If user is not logged in redirect to login page

I appreciate what you have shown me so far, I have learned much from it and I see the direction that you are going with the previous.

$logged = (isset($_SESSION['user_id'])) ? $_SESSION['user_id'] : 0;
$cat = fetch_array(...); //your user-defined function
if($cat['cat_type'] == 0)
{
$content = show_category($cat); //use a function to avoid code duplication
}else{
$content = (logged) ? show_category($cat) : 'Please log in to see this category';
}
function showCategory($cat)
{
$output = ...;
return output;
}
//further on
<div id="content">
echo $content;
</div>

but I don't know how to put it together.

Since this week I have been studying my php pages extensiveley and I have found that most access to restricted information is funneled through a single action page of which seems to use the case switch or statement. Could we intergrate and add to this page with the cat_type switch we created?

You can ask me questions, your guidance is accepted.

Member Avatar for diafol

I'm really sorry, but this is too confusing for me to get a handle on again.

If I take it back to the start and try to make sense of your db tables and forms/links and the input data expected.

Give the structure of your relevant DB tables (e.g. deals, categories and their fields)

What are the links / forms on the page that pass data to the server?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.