We help IT Professionals succeed at work.

PHP Arrays and in_array() function

awarraic
awarraic asked
on
I am getting this error when I compare the data:
Warning: in_array() [function.in-array]: Wrong datatype for second argument in D:\Web\vhosts\dev\mark.php on line 11

Here's how I am saving the data into the array on page one:
while($row = mysql_fetch_array($rs))
{
$_SESSION['mark_array'] = $row["mark_id"];
}

Here's how I am comparing the data on page two:
if (in_array($mark_id, $_SESSION['mark_array'])) echo "not in array";

Any clues? why it's giving that error above
Comment
Watch Question

Commented:
That's easy

$_SESSION['mark_array'] is not an array

$_SESSION is the array

if (in_array($mark_id, $_SESSION)) echo "not in array";

Author

Commented:
somehow the array is not working, nothing shows up on page 2, I printed the output on page 1, works fine but... anyway, I am gona try something else. I am just going to pass a value through session variable.
How do I add a session variable instead of the variable $id bellow in the code.
Thanks much.

Print "<td><a href=/content/mark.php?vendor_id=$vendor_id&amp;mark_id=$id>"; echo $mark_name; "</a></td>";

Commented:
on page 2 do you have reference to the session variable.

<?php
session_start();
$variable = $_SESSION['mark_array'];
?>

Author

Commented:
Yea I do.
I am just trying to figure out how to combine html code with php code and pass a session variable along with query string. for example in the code bellow
Print "<td><a href=/content/mark.php?vendor_id=$vendor_id&amp;mark_id=$id>"; echo $mark_name; "</a></td>";

I want to pass $_session['variable_name'] instead of "$id" in the code above.

Commented:
post the code of page 2.

Author

Commented:
Here's the page 2 code:
<?
session_start();
$mark_id = trim($_GET['mark_id']);
$vendor_id = trim($_GET['vendor_id']);

echo $mark_id;
echo $_SESSION[0];
echo $_SESSION[1];
echo $_SESSION[2];
echo $_SESSION[3];
echo $_SESSION[4];
//echo $vendor_id;
//echo  $_SESSION['logged_in'];
//echo $_SESSION['mark_array'][2];
//if (!in_array(trim($mark_id), $_SESSION)) header("Location:login.php");
// or (!$vendor_id) or (!$mark_id)

But, can I just get the help in just the question bellow.
I want to pass $_session['variable_name'] instead of "$id" in the code bellow.
Print "<td><a href=/content/mark.php?vendor_id=$vendor_id&amp;mark_id=$id>"; echo $mark_name; "</a></td>";

Commented:
you can't get variable name, just the variable value from the $_SESSION variable.

Author

Commented:
Dude, are you even reading my coments/question?
This is what I am looking for if you can help me please, if not thanks for your time.
Can I just get the help in just the question bellow.
I want to pass $_session['variable_name'] instead of "$id" in the code bellow.
Print "<td><a href=/content/mark.php?vendor_id=$vendor_id&amp;mark_id=$id>"; echo $mark_name; "</a></td>";
Commented:
Sorry, it's they way you asked the question.

Print "<td><a href=/content/mark.php?vendor_id=$vendor_id&amp;mark_id=".$_SESSION['vairable_name'].">"; echo $mark_name; "</a></td>";

Commented:
you need to define $_SESSION['variable_name'] somewhere though, perhaps on page1
Print "<td><a href=/content/mark.php?vendor_id=$vendor_id&amp;mark_id=".$_session['variable_name'].">"; echo $mark_name; "</a></td>";

But the whole point of session variables is so that you don't have to pass them from page to page using query strings or form posts... on the second page you could do this:

session_start();
$id=$_session['variable_name'];

and you wouldn't have to pass it on through the URL

Explore More ContentExplore courses, solutions, and other research materials related to this topic.