简体   繁体   中英

Using Session Array in 2 PHP?

I have a PHP in which i the list of few stores based on the user input as username and password.it is showed in the php below:

PHP

<?php  
include('includes/config.php');
 $upload = 'uploads/';
  session_start();
  if(isset($_POST["userid"],$_POST["pid"]))
{   
    $userid = $_POST["userid"];
    $pid = $_POST["pid"];

    $sql = "SELECT * FROM tbl_store WHERE username = '$userid' ";

    $result = mysqli_query($conn,$sql);
    $rowcount=mysqli_num_rows($result);
    $x = 1;
         echo "List of Stores for User".'  '."$userid".'<br/>';
        while($row = mysqli_fetch_array($result)){
            echo $x.'<br/>';

            //$row['store_code'].'<br/>'.'<br/>';
            echo "Store Code".'<br/>';
                        echo '<a href="jobs.php"/>'.$row['store_code'].'</a>'.'<br/>';
                        $_SESSION['$store_code'][] = $store_code;//is the format correct here for SESSION ARRAY??           



                        echo "Store Address".'<br/>';
                        echo $row['store_address'].'<br/>';
            echo "Store Chain".'<br/>';
                        echo $row['store_chain'].'<br/>';

                        echo "-----------------------------------------------------------------------------------------------------".'<br/>'.'<br/>';
            $x = $x +1;
        }

    }
    ?>

For Example.

List of Store Code

1. 12345   //with hyperlink to another PHP

2. 123456 // with hyperlink to another 

3. 1234567 // with hyperlink


Now when I click  1.. Its is re-directed to another page.

Store Code: 12345

List of Jobs:

J1 

J2

J3

PHP for jobs.php where i want to use SESSION Array from previous PHP to get the details from the database based on the first hyperlink clicked.

<?php  
include('includes/config.php');
 $upload = 'uploads/';



    $sql = "SELECT * FROM tbl_job WHERE store_code = 'From the previous PHP' ";

    $result = mysqli_query($conn,$sql);
        $rowcount=mysqli_num_rows($result);
    $job = array();
    $client = array();
    $brand = array();
    $week = array();
    $imgCnt = 1;
    $x = 1;
    echo "LIST OF JOBS ".'<br/>'.'<br/>'; 
    while($row = mysqli_fetch_array($result)){
        echo $x.'<br/>';
        echo "Jobs".'<br/>';
         echo $row['jobs'].'<br/>'.'<br/>'; 
         // How to use Session array here to store 

         echo "Clients".'<br/>';
         echo $row['Client'].'<br/>'.'<br/>'; 


         echo "Brands".'<br/>';
         echo $row['Brand'].'<br/>'.'<br/>';

         echo "WEEK".'<br/>';
         echo $row['week'].'<br/>'.'<br/>';      


?>
    <form style = '<?php echo $imgCnt; ?>' id='uploadForm-<?php echo $imgCnt; ?>' action = '' method = 'POST'>
        <input type="file"  class="image<?php echo $imgCnt; ?>" name="img" onChange="readURL(this);" />
        <img id="blah" src="#" alt="your image" /><br/><br/>
        <input type='button' id = '<?php echo $imgCnt; ?>' class='uploadPicture<?php echo $imgCnt; ?> btn btn-primary' value = 'Upload'>
        <!-- <input type="button" value="upload" class="uploadPicture"  id="upload_btn<?php echo $imgCnt; ?>"/> -->
    </form>
    <form>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

<script>

function readURL(input) {      
        if (input.files && input.files[0]) {
        var reader = new FileReader();
        reader.onload = function (e) {
            //$('#blah').attr('src', e.target.result).width(300).height(340);
             $(input).next('img').attr('src', e.target.result).width(300).height(340);
        };
        reader.readAsDataURL(input.files[0]);
    }

  }
</script>


<script type="text/javascript">
       $('.uploadPicture<?php echo $imgCnt; ?>').unbind().click( function(e) { 

        var file_data = $('.image<?php echo $imgCnt; ?>').prop('files')[0];   
        var form_data = new FormData();                  
        form_data.append('file', file_data);
        var edit_id = $(this).attr("id");
        $.ajax({
                url: "file.php",
                dataType: 'text',  // what to expect back from the PHP script, if anything
                cache: false,
                contentType: false,
                processData: false,
                data: form_data,                         
                type: 'post',
                success: function (result) {
                        alert(result)

                }
            });
        });
    </script>




                <?php
                echo "------------------------------------------------------------------------------------------------------".'<br/>';
         $x = $x + 1;
         $imgCnt++;
    }


?>

here is the screenshot of the page how it looks like. 在此处输入图片说明

Now when i upload the image from the PHP, it should be moved to folders and subfolders created in the format

Client/Brand/Week/Job/Client_Brand_Week.jpg

Can you please help me with this.

PHP for file.php

<?php

    $imagePath = "uploads/";
    $temp = explode(".", $_FILES["file"]["name"]);

    $extension = end($temp);

    $filename = $_FILES["file"]["tmp_name"];
    echo "$filename";
    $time = time();
    move_uploaded_file($filename, $imagePath . $time . '.' . $extension);    
   echo "File Uploade";
   exit;
 ?>

in the first PHP file:

echo "Store Code".'<br/>';
echo '<a href="jobs.php"/>'.$row['store_code'].'</a>'.'<br/>';
$_SESSION[$userid]['store_code'][] = $store_code;  //<-- this should work  
echo "Store Address".'<br/>';
echo $row['store_address'].'<br/>';
echo "Store Chain".'<br/>';
echo $row['store_chain'].'<br/>';
echo "-----------------------------------------------------------------------------------------------------".'<br/>'.'<br/>';
$x = $x +1;

but please be aware that you have not set the variable $store_code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM