Jump to content

help with classified ads code


gadgetster
 Share

Recommended Posts

i keep getting an error.

 

posting the error message, with the line number mentioned in it, would allow someone to help you. as it is, all you are have done is dump your code on a forum and that won't get you much help since we don't have the slightest idea what you saw in front of you that would narrow down the problem.

Link to comment
Share on other sites

It might help to post the code instead of attaching a ZIP file. When posting code, please surround it with


tags.

 

Also, when PHP says there's an error on a specific line, it doesn't always mean that the error is on that line. Perhaps something before line 2 is causing the issue. 

Link to comment
Share on other sites

 

the category class definition is incorrect. it should start with -

<?php
class Category
{

ok, i fixed that and i get the following error:

syntax error, unexpected 'list' (T_LIST), expecting identifier (T_STRING) in C:\wamp\www\test\models\category.php on line 43

 

here is my models/category.php code:

<?php
class Category
{
   function __construct()
   {
      // for this tutorial there will be nothing inside this method
      // in the real app you could possibly add some initialization
   }

   function add($name)
   {
      $this->filter($name);
      $q="INSERT INTO categories (name) VALUES (\"$name\")";
      mysql_query($q) or die(mysql_error());
   }

   function edit($name, $id)
   {
      $this->filter($name);
      // add some basic security
      if(!is_numeric($id)) exit;

      $q="UPDATE categories SET name=\"$name\" WHERE id='$id'";
      mysql_query($q);
   }

   // category name allows only alphanumeric
   private function filter($name)
   {
      $name=preg_replace("/\W/","",$name);
      return $name;
   }

   function delete($id)
   {
       if(!is_numeric($id)) exit;

       $q="DELETE FROM categories WHERE id='$id'";
       mysql_query($q);
   }

   // method to list the categories for admin edit/delete
   function list()
   {
       $q="SELECT * FROM categories ORDER BY name";
       $result=mysql_query($q);

       $cats=array();

       while($cat=mysql_fetch_array($result)) $cats[]=$cat;
       return $cats;
   }

   // method to return categories along with number of ads in them
   function list_count()
   {
       $q="SELECT COUNT(tA.id) as classifieds, tC.name as name, tC.id as id
       FROM categories tC LEFT JOIN classifieds tA ON tA.category_id=tC.id
       ORDER BY tC.name";
       $result=mysql_query($q);
       $cats=array();

       while($cat=mysql_fetch_array($result)) $cats[]=$cat;
       return $cats;
   }

   // method to return full category data for a single category
   function select($id)
   {
      if(!is_numeric($id)) exit;

       $q="SELECT * FROM categories WHERE id='$id'";
       $category=mysql_fetch_array(mysql_query($q));
       return $category;
    }
}
?>
Link to comment
Share on other sites

obviously the author of that site didn't write/test this code and you should not waste any more time trying to use it unless you are trying to gain experience debugging php syntax errors.

 

its not this forum's function to debug a bunch of syntax errors in code that was found on the Internet.

Link to comment
Share on other sites

your lack of time to learn the basics isn't our problem. for the basics, you need to spend your time and maybe some money to buy and read a book, take a class, hire a tutor, or study enough online tutorials to learn the prerequisite information for the task you are trying to accomplish.

 

programming help forums are for people to get help with code they have written. if you are not at the point where you can even get past simple punctuation and language errors in any code and are just posting error after error you are getting in some code you have found, you are not at the point to even be asking for help in a programming forum, because you haven't programmed anything.

 

you need to either a) learn enough of the programming language you are using so that you don't need to be spoon fed in post after post, or b) hire someone to write the code that does what you want.

 

and finally, since this is some third party code and not your own, moving this thread to the Third Party Script Forum Section...

Link to comment
Share on other sites

I agree with the Guru member - you need to learn the basics of programming in general and then PHP. I ran your code and there are multiple issues. You are not accessing the database columns properly for one:

$cats[] = $cat 

 should be:

$cats[] = $cat['some_column_in_table'] 

Also, you don't use opening and closing braces properly or return values on several functions. 

   function list()
   {
       $q="SELECT * FROM categories ORDER BY name";
       $result=mysql_query($q);

       $cats=array();

       while($cat=mysql_fetch_array($result)) $cats[]=$cat;
       return $cats;
   }

should be more along the lines of this:

   function list() {

       $q="SELECT * FROM categories ORDER BY name";

       $result=mysql_query($q);

       $cats=array();

       while($cat=mysql_fetch_array($result)) {

           $cats[]=$cat['some_column_in_table'];
       }

       return $cats;
   }

You should also be using PDO to access the database as your code has been deprecated and will not work in PHP future releases.

Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.