0

I have this code:

if ( !isset($_REQUEST['term']) )
    exit;

$dblink = mysql_connect('localhost', 'database', 'password') or die( mysql_error() );
mysql_select_db('database');

$rs = mysql_query('SELECT name, producator, id, tip FROM jocuri2 WHERE name LIKE "'. mysql_real_escape_string($_REQUEST['term']) .'%" order by id asc limit 0,5', $dblink);

$data = array();
if ( $rs && mysql_num_rows($rs) )
{
    while( $row = mysql_fetch_array($rs, MYSQL_ASSOC) )
    {
        $data[] = array(
            'label' => $row['tip'] .'/ '. $row['name'] .'/ '. $row['producator'] ,
            'value' => $row['name']

        );

    }
}

echo json_encode($data);
flush();

So, when I'm looking for a name, this script return complete name, but when I click it, search box receive name value. How can I do, when I click on item to move me to item page. Every item have profil.php?id=(page).

2 Answers 2

1

First of. Don't use the mysql module, it's been deprecated and is unsafe to use. Switch to msqli or pdo.

It's abit hard to understand what you are trying to do, this script only returns a json. The click functionality would be handled in javascript or similar.

However: You are missing some kind of id reference to you data. Your json will only return the name and a label and your "click" function will not know what id to send to the profil.php

I would change

while( $row = mysql_fetch_array($rs, MYSQL_ASSOC) )
    {
        $data[] = array(
            'label' => $row['tip'] .'/ '. $row['name'] .'/ '. $row['producator'] ,
            'value' => $row['name']

        );

    }

to

while( $row = mysql_fetch_array($rs, MYSQL_ASSOC) )
    {
        $data[] = array(
            'id' => $row['id'],
            'label' => $row['tip'] .'/ '. $row['name'] .'/ '. $row['producator'] ,
            'value' => $row['name']

        );

    }

And then reference the data.id in my javascript-file.

Sign up to request clarification or add additional context in comments.

1 Comment

I modified this . But i still don't know how to do what i wanna...After this array, i want click on searched item and then redirect me to item page...
0

Here you need to understand array...

array always content two things in an element...['key'] => ['value']

e.g. $prod = array ("1"=>"Fruit","2"=>"Vehicles")

so as per above example you are not passing page id in array..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.