Jump to content

ok to do?


blueman378

Recommended Posts

hi guys, well i have a function inside a class, and if i call it through:

 

$prodadmin->view_prod($cid); it will show only the products from the category with the id cid,

 

however if i call it as

 

$prodadmin->view_prod();

 

it gets all products, this is what i want however i get an error saying missing arguements,

 

so i call it as

@$prodadmin->view_prod();

 

that way i dont have to have two functions, and it doesnt show the error, is it ok to do this or should i just write two codes?

 

Link to comment
https://forums.phpfreaks.com/topic/108677-ok-to-do/
Share on other sites

You'll want to use null as a default then check this within your function to execute the appropriate query. eg;

 

<?php

  function view_prod($id=null) {
    if (is_null($id)) {
      $sql = "SELECT * FROM foo";
    } else {
      $sql = "SELECT * FROM foo WHERE id = $id";
    }
    // ...
  }

?>

Link to comment
https://forums.phpfreaks.com/topic/108677-ok-to-do/#findComment-557410
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.