I'm trying to make a class registration page, but for some reason, when I dump my query results into an array and use array_search on it, array_search comes up null. Any ideas?

<?php
  
  	/* - if classes[] is set, see if any of them were selected
		   - if yes, insert the class into the grades table with the student's GWid and grade of IP(in progress).
		      if the course was already in the grades table with an NG grade, change the grade to IP.
		   - if no, do nothing
	   - if it is not set, do nothing
	*/
	$nextterm = $_SESSION['nextterm'];
	if($nextterm == 'spring')
	{
	    $termyear = (date('Y') + 1);
	}
	else
	{
	    $termyear = date('Y');
	}
	
	if(isset($_POST['classes']))
	{
		$sql = "SELECT classid FROM grades WHERE GWid=$GWid AND grade='NG'";
					
			$result = mysql_query($sql);
			
			if(!$result)
			{
				die("Error querying database:" . mysql_error());
			}
		
		list($ngclasses) = mysql_fetch_array($result);

		foreach($_POST['classes'] as $key => $cid)//for every class that gets posted:
		{
/*****************************************************************
********            Issue starts here      *********************************
******************************************************************
*/			
			if(array_search($cid, $ngclasses))//this comes up null
			{//change NG grade to IP
				$sql = "UPDATE grades SET grade='IP' WHERE GWid=$GWid AND classid='$cid'";
			}
			else
			{//insert class into grade table with in progress grade (IP)
				$sql = "INSERT INTO grades(GWid, classid, grade, semester, year, required) VALUES(" 
				       . $_SESSION['GWid'] . ", '$cid', 'IP', '$nextterm', '$termyear', 'no')";
			}
					
			$result = mysql_query($sql);
			
			if(!$result)
			{
				die("Error querying database:" . mysql_error());
			}
		}
					
		/*$result = mysql_query($sql);
			
		if(!$result)
		{
			die("Error querying database:" . mysql_error());
		}
		*/
		header("Location: viewsched.php");
	}
  ?>

Recommended Answers

All 6 Replies

list($ngclasses) = mysql_fetch_array($result);

is wrong. It will assign the 0th index of the array fetched by $result to a variable $ngclasses. If you want $ngclasses to be an array, don't use list. Well, here is more on list.
http://nl2.php.net/manual/en/function.list.php

So should I use array($ngclasses) = mysql_fetch_array($result); instead?

No.. Just use $ngclasses = mysql_fetch_array($result);

I tried using $ngclasses = mysql_fetch_array($result); but array_search($cid, $ngclasses) is still coming up null. Am I using array_search wrong?

------------------
Another issue is that this chunk of code I wrote for debugging purposes:

$sql = "SELECT classid FROM grades WHERE GWid=$GWid AND grade='NG'";
			
	$result = mysql_query($sql);
	
	if(!$result)
	{
		die("Error querying database:" . mysql_error());
	}
	
	$ngclasses = mysql_fetch_array($result);
	for($i = 0; $i < count($ngclasses); $i++)
	{
	    echo(count($ngclasses)." ngclasses: ". $ngclasses[$i] . "<p>");
	}

Doesn't give the right results. The query comes up with 4 $cids when I do it directly in phpMyAdmin, but the php page gives this output:

2 ngclasses: CS284

2 ngclasses:

I ended up solving it. array_push() was the magic function here. It was pretty complicated, but this made it work:

$ngclasses = mysql_fetch_array($result);
		while(list($ngc) = mysql_fetch_row($result))
		{
		    array_push($ngclasses, $ngc);
		}
        $classlist = $_POST['classes'];
		foreach($classlist as $key => $cid)//for every class that gets posted:
		{
		    if(!(array_search($cid, $ngclasses) === FALSE))
			{
		        $sql = "UPDATE grades SET grade='IP' WHERE GWid=$GWid AND classid='$cid'";
			}	
			else if(array_search($cid, $ngclasses) === FALSE)
			{
			    $sql = "INSERT INTO grades(GWid, classid, grade, semester, year, required) VALUES(" 
				       . $_SESSION['GWid'] . ", '$cid', 'IP', '$nextterm', '$termyear', 'no')";
			}
					
			$result = mysql_query($sql);
			
			if(!$result)
			{
				die("Error querying database:" . mysql_error());
			}
		}

Congrats! Don't array_push insert an element to the array ? Weren't you searching the array using array_search ? Anyways, Congrats once again !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.