Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8633249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:27:51+00:00 2026-06-12T09:27:51+00:00

I am attempting to identify whether a variable in an array is present, and

  • 0

I am attempting to identify whether a variable in an array is present, and if it is state that it is “true” and if it is not state that it is “false”. The problem is that I am receiveing a response for all array variables, when I only want to know if the page variable is present in the array with link like http://example.com/product.php?image_id=50

<?php 
$page = $_GET['image_id'];
if (!empty($_SESSION)){
foreach($_SESSION as $name => $value2)   {
  if (substr($name, 0, 7)=='images_') {
  $id = substr($name, 7, (strlen($name)-7));
  $get = mysql_query('SELECT * FROM images WHERE image_id='.mysql_real_escape_string((int)$id));
     while ($cart_info_row = mysql_fetch_assoc($get)) {
        $cart = array('image_id' => $cart_info_row['image_id'],);
        if (in_array($page, $cart)){echo 'true';} else {echo 'false';}
     }
  }
  }
  } 
  }   
?>

Assuming the numbers below are in the SESSION print_r ($cart):

Array ( [image_id] => 42 ) false

Array ( [image_id] => 45 ) false

Array ( [image_id] => 50 ) true

Array ( [image_id] => 49 ) false  

Now, how can I ONLY identify if variable 50 ($page = $_GET['image_id'];)is present with out the other three?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T09:27:52+00:00Added an answer on June 12, 2026 at 9:27 am

    As I understand, your code does 2 things: 1 – checks if key images_$page exists in session and 2 – checks if image with image_id=$page exists in database.

    <?php
    $page = $_GET['image_id'];
    if(isset($_SESSION['images_'.$page]))
    {
        $get = mysql_query("SELECT * FROM images WHERE image_id='".mysql_real_escape_string($page)."' LIMIT 1");
        if($cart_info_row = mysql_fetch_row($get))
        {
            echo 'true';
        }
        else
        {
            echo 'false';
        }
    }
    else
    {
        echo 'false';
    }
    

    Here isset is used to check if the key images_$page exists in $_SESSION. It is faster than making 2 calls to substr() function.

    Also, since we need only checking if given image exists, foreach loop is replaced with if.

    After that, a MySQL query runs, and since we need only first match, LIMIT 1 is added to make query faster. Also comparison with image_id is already included in query, so its enough to test if there are returned rows or not (that’s why original while is replaced with if).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Not long ago I asked a question attempting to identify a certain unicode character
I am attempting to identify the ether type of a packet that I am
I am attempting to identify a jQuery plugin that will display the a full
I'm attempting to merge multiple SQL Server databases into one. The problem is that
I'm attempting to identify the blocks that are tied to a specific file and
Attempting to make a NSObject called 'Person' that will hold the login details for
Attempting to implement a poor man's test of whether a process is still running
I am simply attempting to get jquery to identify the first letter of a
Attempting to upload a video, and this is the response I get back (after
I am essentially attempting to replace all of the footnotes in a large text.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.