I have two arrays, and my problem is when I pick one value of first array it must also remotely picked the same value of the second array.

Explain :

array ("Volvo","BMW","Toyota");

array ("200","300","400");

It's like when I choose "Volvo" it must also remotely choose "200" OR when I choose "BMW" it must also remotely choose "300" and so on...

how can I do this please help;

thanks for your help..

Recommended Answers

All 4 Replies

Member Avatar
$value = $_POST['car']; //assuming coming from form

$cars = array ("Volvo","BMW","Toyota");
$nums = array ("200","300","400");

$index = array_search($value, $cars);
$num = $nums[$index];

echo $value . ': ' . $num; //test

I tried to expand your code on a query..



    $prod_id = $_POST['prod_id'];
    $value = $_POST['sizeStatus']; //assuming coming from form

     $sql1 = "select * from tbl_product where prod_id = $prod_id";//SQLquery

    $result1 = mysqli_query($con,$sql1);
    $row = mysqli_fetch_array($result1);

    $sizes = $row['sizes'];//assuming coming from database : 1, 2, 3
    $colour = $row['colors'];//assuming coming from database : red, blue, pink 

    $arraySize = explode(',',$sizes);
    $arrayColour = explode(',',$colour);
    $index = array_search($value, $arraySize);
    $num = $arrayColour[$index];
    echo $value . ': ' . $num; //test 


<!---// FORM  //-->
<form method="post" action=""> 
        $sql = "select * from tbl_product where prod_id = 468"; //SQLquery
        $result = mysqli_query($con,$sql);
        $row = mysqli_fetch_array($result);
        $prod_id = $row['prod_id'];
        $sizes = $row['sizes'];

        echo '<input type="hidden" name="prod_id" value="'.$prod_id.'">';
        $temp = explode(',',$sizes);
        echo "<select name='sizeStatus'>";
        foreach($temp as $size){
        echo "<option>".trim($size)."</option>";
        echo "</select>";

<input type="submit" name=" submit">

the SIZE VALUE was right .. but the COLOUR VALUE is always RED .. why please help ..

why are you exploding sizes? How do you have the size column defined? Try not to mix php with html if you can.

<form method="post" action="">
    $sql = "SELECT * FROM tbl_product WHERE prod_id = '468'";
    $results = mysqli_query($con, $sql);

    $row = mysqli_fetch_array($results);

    $sizes = explode(',',$row['size']);

<input type="hidden" name="prod_id" value="<?php echo $row['prod_id']; ?>" />

<select name="sizeStatus">
<?php foreach($sizes as $size): ?>
    <option value="<?php echo trim($size); ?>"><?php echo trim($size); ?></option>
<?php endforeach; ?>
<input type="submit" name=" submit">

Also give your option a value like above.

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.