Welcome Guest, Not a member yet? Register   Sign In
wondering how do i go about getting this to work
#1

I am trying to add a shop to my site for my members. but i am not sure on how to do this in the model tho.

i can somewhat guess on how to do it.
this is how i was thinking of going about it:

Code:
my ideal of the code:

$query = $this->db->query("ELECT * FROM `shop_items`");
if ($query->num_rows() == 0)
{
return "error";

}else
{
return $query->result_array();
}

$logins[username] would be used in view

but this below is from a tutorial site that does mysql authorization scripts and i am trying to convert it to codeigniter.

Code:
site:
http://tutorial.ninja/tutorials/Web+Development/PHP+-+User+System/283/Shop+Makeover.html

this is where i got the code from. i got everything else working. register, login, members page, edit page. its just i don't know how to figure best route to go for the shop option. (I know codeigniter has a cart class/helper. i dont want to use it).
Code:
code need help with
<?php
print "Welcome to the shop $logins[username]."; //welcome ^^
           $get_items = $this->db->query("SELECT * FROM `shop_items`"); //get all items
           if($get_items->num_rows == 0){ //check if any in db
               print "Sorry, No Shop Items Available.";
           }else{
               while($items = $get_items->row_array()){
                   if($items['staffonly'] == "True" && $logged['userlevel'] >= '4'){ //staff items :D
                       print "<h2>Staff Items</h2>";
                       print "<img src=\"$items[image]\" alt=\"$items[name]\" />
                       <table width=\"400\">
                       <tr>
                       <td width=\"400\" colspan=\"4\" align=\"left\" valign=\"middle\">
                       <b>$items[name]</b>
                       </td>
                       </tr>
                       <tr>
                       <td width=\"100\" align=\"left\" valign=\"middle\">
                       <b>Description</b>
                       </td>
                       <td width=\"300\" align=\"center\" valign=\"middle\">
                       $items[desc]
                       </td>
                       </tr>
                       <tr>
                       <td width=\"100\" align=\"left\" valign=\"middle\">
                       <b>Price</b>
                       </td>
                       <td width=\"300\" align=\"center\" valign=\"middle\">
                       $items[price] Point(s)
                       </td>
                       </tr>
                       <tr>
                       <td width=\"100\" align=\"left\" valign=\"middle\">
                       <b>Quantity</b>
                       </td>
                       <td width=\"300\" align=\"center\" valign=\"middle\">
                       $items[quantity]
                       </td>
                       </tr>
                       <tr>
                       <td width=\"100\" align=\"left\" valign=\"middle\">
                       &nbsp;&nbsp;
                       </td>
                       <td width=\"300\" align=\"center\" valign=\"middle\">
                       <a href=\"shop.php?page=verify&item=$items[id]\">Buy</a>
                       </td>
                       </table>"; //print item data
                   }elseif($items['staffonly'] == "True" && $logged['userlevel'] < '4'){
                       //nothing here cause admins wont be able to see it ;(
                   }else{ //not an admin and item aint staff only
                       print "<img src=\"$items[image]\" alt=\"$items[name]\" />
                       <table width=\"400\">
                       <tr>
                       <td width=\"400\" colspan=\"4\" align=\"left\" valign=\"middle\">
                       <b>$items[name]</b>
                       </td>
                       </tr>
                       <tr>
                       <td width=\"100\" align=\"left\" valign=\"middle\">
                       <b>Description</b>
                       </td>
                       <td width=\"300\" align=\"center\" valign=\"middle\">
                       $items[desc]
                       </td>
                       </tr>
                       <tr>
                       <td width=\"100\" align=\"left\" valign=\"middle\">
                       <b>Price</b>
                       </td>
                       <td width=\"300\" align=\"center\" valign=\"middle\">
                       $items[price] Point(s)
                       </td>
                       </tr>
                       <tr>
                       <td width=\"100\" align=\"left\" valign=\"middle\">
                       <b>Quantity</b>
                       </td>
                       <td width=\"300\" align=\"center\" valign=\"middle\">
                       $items[quantity]
                       </td>
                       </tr>
                       <tr>
                       <td width=\"100\" align=\"left\" valign=\"middle\">
                       &nbsp;&nbsp;
                       </td>
                       <td width=\"300\" align=\"center\" valign=\"middle\">
                       <a href=\"shop.php?page=verify&item=$items[id]\">Buy</a>
                       </td>
                       </table>"; //print item data
                   } //end level check >]
               } //end loop for items >/
           } //end item check

?>
Reply
#2

You should probably use one of the existing auth libraries for CodeIgniter. Authentication and security will ruin a web shop if anything goes wrong.

Generally, tutorials that aren't written specifically for CodeIgniter (or at least some MVC framework) are only going to help with the concepts of how to handle an issue. The code you've posted mixes database queries with raw text and deprecated HTML. In MVC the database query should be in the model or controller and the HTML in a view, and, if you're creating a new site, you should take the time to learn/write modern HTML (in the end, it will require less code than your example, too).

The first line of code you posted contains a typo in the database query.

In the last block of code you've included, you're testing a value against the string "True" instead of using a boolean value, then comparing another value against another string using less/greater than operators, which can give you some unexpected results.
Code:
if($items['staffonly'] == "True" && $logged['userlevel'] >= '4'){

If you made 'staffonly' a boolean value in your database, it would require less memory/storage and the test would be less subject to string comparison issues (like capitalization and encoding). If 'userlevel' could be numeric, it would have similar benefits in most cases (since it rarely requires more memory to store the number 4 than the character 4).

Code:
if ($items['staffonly'] === TRUE && $logged['userlevel'] >= 4) {

Additionally, you probably need to put braces around your variables in your strings when the variables are array references, and you need to quote the names of the keys. So, for example, the following:
Code:
print "<img src=\"$items[image]\" alt=\"$items[name]\" />";

should probably be something like this:
Code:
print "<img src=\"{$items['image']}\" alt=\"{$items['name']}\" />";

(I inserted the closing quote and semi-colon to avoid trying to deal with the table which followed it.)

Or you could make it potentially clearer like this:
Code:
print "<img src='{$items['image']}' alt='{$items['name']}' />";

or use row() instead of row_array() and change it to:
Code:
print "<img src='{$items->image}' alt='{$items->name}' />";
Reply
#3

Code:
$items['staffonly'] == "True"
is a value in the database tho.

what its suppose to do is pretty much if your rank is 4,6 it will only show staff items and if your not moderator(4) and/or admin(6) it won't show staff items.

its being alil hard to figure what to use to only show staff items to staff members and not to staff members
Reply




Theme © iAndrew 2016 - Forum software by © MyBB