PHP logic confusion [ERROR]

I have a table in the database. The table structure is given in the next line.
These are the columns.

  • nid,ntitle,nbody,nauthor,nreads,ncategory.

Now,i have suceeded in pulling out all rows from the database with this query.
SELECT * FROM dbtable DESC LIMIT 10.
.
The above query ,pulls out news rows from database table regardless of category the news belongs to and the news is displayed in the index.php page.

Now, my logic is that i want to pull out news rows from the database table which belong to a specific category.

SELECT * FROM dbtable where pcategory = cat_id;

In the index.php file i want to make the code works in a different way.Say for example when a request comes to index.php file in this way ( index.php?open_cat=12 where open_cat=12 is the category id)
So,i made the below code.


<?php
include("function.php");
if(isset($_GET['open_cat']))
                         {
                            $id = $_GET['open_cat'];
                            $id = mysql_real_escape_string($id);
                           getnews($id);
                         }
                         else
                         {
                         getnews();
                         }


?>

I want the function getnews($id) to fetch only news from the database table whose category is equal to $id.

I want the function getnews() to simply fetch all news from the database table regardless of the category.

This is my function.php file.


<?php

function getnews($id)
{
if($id!=null){
//then get news which belongs to category $id from database table.
}
else{
//get news from database table regardless of which category it belongs to.
}
}

?>

My question is is this possible? I meant to call getnews() method but getnews() method returning different results based on the parameters it received.

Im confused,i’ve been trying this since yesterday.

Thanks and Regards.

i said it’s safer cause i filtered and forced the argument as a int type, not means the way i getting the argument is safer

You should only use func_get_arg() and friends when your function needs to be designed to use a variable number of arguments.

In this case, the only way you would want to use func_get_arg() is if you wanted to pass multiple news ids to getnews.

getnews(1, 2, 3);

Realistically, you’d still probably be better off passing an array.

The type-casting though, I do agree with. I would rather see this function definition than the one using a boolean. zero is evaluated to false anyways.

function getnews($nid = 0)
{
	$nid = (int)$nid; // Make sure it's a number

	if($nid) // Non-numbers will be evaluated to zero on the line above, and therefore not trigger this
	{
		// Get $result
		return $result;
	}

	// Get results
	return $results;
}

The way i see it declaring function arguments is a lot quicker and uses less memory then using 2/3 more functions to get the arguments you need. Also i fail to see how its safer as your still getting the same value from the argument if you did it the normal way

i think you can do it in this way:


function getNews(){
    if(func_num_args() == 1){
           $id = intval(func_get_arg(0));
           //select news from the category $id refers
    }else{
           //select news from all categories
    }
}

it’s safer and easy to add some other arguments

thanks alot. that works exactly as i had plan.

In your function change it to this

function getnews($id=false)
{
    if ($id){
        //then get news which belongs to category $id from database table.
    }
    else{
        //get news from database table regardless of which category it belongs to.
    }
}

which will only get all the news articles if the argument $id is not equal to false