[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

PHP Arrays and in_array() function

Posted on 2008-01-25
11
Medium Priority
?
304 Views
Last Modified: 2013-12-12
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
0
Comment
Question by:awarraic
  • 6
  • 4
11 Comments
 
LVL 17

Expert Comment

by:nplib
ID: 20746273
That's easy

$_SESSION['mark_array'] is not an array

$_SESSION is the array

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

Author Comment

by:awarraic
ID: 20746417
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>";
0
 
LVL 17

Expert Comment

by:nplib
ID: 20746430
on page 2 do you have reference to the session variable.

<?php
session_start();
$variable = $_SESSION['mark_array'];
?>
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:awarraic
ID: 20746460
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.
0
 
LVL 17

Expert Comment

by:nplib
ID: 20746476
post the code of page 2.
0
 

Author Comment

by:awarraic
ID: 20746530
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>";
0
 
LVL 17

Expert Comment

by:nplib
ID: 20746542
you can't get variable name, just the variable value from the $_SESSION variable.
0
 

Author Comment

by:awarraic
ID: 20746569
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>";
0
 
LVL 17

Accepted Solution

by:
nplib earned 750 total points
ID: 20746586
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>";
0
 
LVL 17

Expert Comment

by:nplib
ID: 20746590
you need to define $_SESSION['variable_name'] somewhere though, perhaps on page1
0
 
LVL 19

Expert Comment

by:darron_chapman
ID: 20746660
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
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

590 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question