Jump to content

"Wrong datatype for second argument" while using in_array()


LOSTBOY

Recommended Posts

No problem at all, but I would suggest to you never use the @ operator to overide an error it becomes very hard to debug, find out why $tempArray is not an array and try to overcome it before the if statement (something like if its not an array, initialize it to an array), it would be a good programming practice

here is my code.....actually i want to generate Randam Quiz's .....as single quiz per page

here $_session['$max'] is the maximum questions that user select from our db

 

if (!isset ($_SESSION['$max'])){
$_SESSION['$max']=$_POST['sel_ques'];
$tempArray= array();
} 
  

do
{   
  $qno=rand(1, $_SESSION['max']);   
  if (!@in_array($qno,$tempArray)) { 
    $tempArray[$r] = $qno;
    $query="SELECT * FROM question WHERE qid=$qno" ;
    $result=mysql_query($query);
    $id=mysql_result($result,$i,"qid");
    $question=mysql_result($result,$i,"question");
    mysql_result($result,$i,"question");
    $query1="SELECT * from answer where qid=$id";
    $result1=mysql_query($query1);
$r++;
  }

 

 

But it not working properly .......plz give ur suggestions

try this code

 

if (!isset ($_SESSION['$max'])){
$_SESSION['$max']=$_POST['sel_ques'];
}

if (!isset($_SESSION['tempArray']))
{
   $_SESSION['tempArray'] = array();
} 

$query = "SELECT * FROM question limit 1";
if (!empty($_SESSION['tempArray']))
{	     
    $query="SELECT * FROM question WHERE qid not in (".implode(",",$_SESSION['tempArray']).") limit 1" ;
}
    $result=mysql_query($query);
    $id=mysql_result($result,1,"qid");
    $_SESSION['tempArray'][] = $id;
    $question=mysql_result($result,1,"question");
    mysql_result($result,$i,"question");
    $query1="SELECT * from answer where qid=$id";
    $result1=mysql_query($query1);

 

and please backup your old code incase I am wrong  :P

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.