andyd34 Posted September 9, 2009 Share Posted September 9, 2009 I have a generic function for searches but I want to add addition fields which are optional. For some reason I thought doing the following would arrow me to do this. function browse($mode, $type, field1 = NULL, $field2 = NULL) { } then to call the function $search_new = browse($_POST['new'], $_POST['games']); $search_new_arcade = browse($$_POST['new'], $_POST['games'], $_POST['arcade']); but this doesnt work, I have seen it done before but cant remember where. Can someone please help so I dont have to call differant functions to output the same things Thanks Link to comment https://forums.phpfreaks.com/topic/173666-functions/ Share on other sites More sharing options...
micmania1 Posted September 9, 2009 Share Posted September 9, 2009 Can you post your function code? Link to comment https://forums.phpfreaks.com/topic/173666-functions/#findComment-915434 Share on other sites More sharing options...
andyd34 Posted September 9, 2009 Author Share Posted September 9, 2009 This is not hte actual function as its over 200 lines long but its more or less what the function is about, the $field1, $field2 etc are supposed to be optional and do not require a value or so i thought function browse($mode, $type, $field1 = NULL, $field2 = NULL) { if($mode=='new') { if($type=='games') { $sql = "SELECT * FROM table WHERE cat = '$type'"; if($field=='arcade' ? " AND sub_cat = $field" : ''); $query = mysql_query($sql) } if($type=='software') { $sql = "SELECT * FROM table WHERE cat = '$type'"; } } } Link to comment https://forums.phpfreaks.com/topic/173666-functions/#findComment-915440 Share on other sites More sharing options...
micmania1 Posted September 9, 2009 Share Posted September 9, 2009 if($field=='arcade' ? " AND sub_cat = $field" : ''); $query = mysql_query($sql) } You need to change your variables to $field1 and $field2 Link to comment https://forums.phpfreaks.com/topic/173666-functions/#findComment-915444 Share on other sites More sharing options...
andyd34 Posted September 9, 2009 Author Share Posted September 9, 2009 yes i know to call the variables is if($field1 ? $str[] = "cell = '$field1'" : ''); if($field2 ? $str[] = "cell = '$field2'" : ''); if($field3 ? $str[] = "cell1 = '$field3'" : ''); if($field4 ? $str[] = "cell2 = '$field4'" : ''); if($field5 ? $str[] = "cell3 = '$field5'" : ''); $where = implode(' AND ', $str); $sql = "SELECT * FROM table"; if($where ? $sql .= " WHERE $where" : ''); the porblem was for some strange reason it was returning an error. I deleted my history and cookie and the error has cleared. Strange!! Link to comment https://forums.phpfreaks.com/topic/173666-functions/#findComment-915459 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.