Thread: 2d Array

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    15
    Rep Power
    0

    2d Array


    I am testing out a cataloging idea.
    I want the person to scan a barcode which will represent a unique item (book, toaster, ash tray, etc). The system will first check a 2d array to see if the barcode already exists in the array. If it does, it simply adds 1 to the total in the array. If it doesn't exist in the array, it will call a PHP form to get additional information on the item such as manufacturer, name, price. It will then return this information to Javascript which will then enter the barcode, manufacture, name, price and quantity into the array.

    What I have so far:

    Code:
    //Delclare array
    var contents = [];
    
    //I have some logic that looks for the enter key to be pressed.
    //Then it looks at what the text of #Commands is to determine 
    //what it should do
    if ($("#Commands").text() == "SCAN BARCODE"){
         //Get the barcode from the input box
         var value = $('input[id=Input]').val();
    			
         //Let's check to see if barcode is already in the array
         if ($.inArray(value, contents) == -1){
                      
               //We have established the barcode is not in the array. 
              //So let's get the sku, pkg and description of the upc
              //It should return an array which we will store in
              //newcontent
              newcontent = getUPCDetails(value);
    
             //Not sure how to insert the contents of array 
             //newcontent into content
    
    	};
         //If the barcode is already in the array, we will simply
         //update the quantity for that barcode
         if ($.inArray(value, contents) != -1){
    			
    			
         };
         
         //We clear the input text box so the user can scan the
         //next barcode
    
         $('input[id=Input]').val('');
    	return false;
    		}

    Details of getUPCDetails():

    Code:
    function getUPCDetails(value){
         //This will call a PHP file and pass it value
         //I would like the PHP file to return an array
        //Not sure how to capture the array
         $.post('getUPCDetails.php', {variable: value}, 
    	
    }
    PHP file:

    Code:
    //Excecute SQL query to get barcode information
    $res = odbc_exec($conn,"SELECT manufacturer,name, price FROM table where barcode = $value");
         if (!$res){
              echo "SQL Error";
         }
         else{
              // If barcode details are found we will loop
             // through them and append them to details array
              while (odbc_fetch_row($res)){
                   $details[0]=odbc_result($res,"manufacturer");
    	  $details[1]=odbc_result($res,"name");
    	 $details[1]=odbc_result($res,"price");
              }
         }
    
    // Once we have the details, we need to send it back
    // to the javascript application	
    echo json_encode(array("result" => $details));
    I am just trying to figure out how to create and populate the 2d array in this scenario.
  2. #2
  3. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,172
    Rep Power
    184
    Here's a little example; basically based on what your wanting to do..., you will just have to implement it into your code:
    Code:
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script>
    
    var found;
    
    var content = {
     "codes": [
      {"barcode":"1234","barcode_count":"1"},
      {"barcode":"1235","barcode_count":"1"}
     ]
    };
    
    function check4code()
    {
    
     var value = parseInt($("input[id='Input']").val());
     $.each(content.codes, function(e){
    
        if(value == content.codes[e].barcode) {
         content.codes[e].barcode_count = parseInt(content.codes[e].barcode_count) + 1;
         found = "yes";
        }
        
       alert("Barcode: "  + content.codes[e].barcode + " | Quantity: " + content.codes[e].barcode_count); // remove - demo purposes only
    
     });
    
     if(found != "yes") {
      content.codes.push({"barcode":value,"barcode_count":"1"});
      alert("Barcode: "  + content.codes[content.codes.length-1].barcode + " | Quantity: " + content.codes[content.codes.length-1].barcode_count); // remove - demo purposes only
     }
    
    found = "no";
    
    }
    
    </script>
    
    <input id="Input"/>
    <input type="button" value="Look Up Barcode" onclick="check4code()"/>
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    15
    Rep Power
    0
    Originally Posted by web_loone08
    Here's a little example; basically based on what your wanting to do..., you will just have to implement it into your code:
    Code:
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script>
    
    var found;
    
    var content = {
     "codes": [
      {"barcode":"1234","barcode_count":"1"},
      {"barcode":"1235","barcode_count":"1"}
     ]
    };
    
    function check4code()
    {
    
     var value = parseInt($("input[id='Input']").val());
     $.each(content.codes, function(e){
    
        if(value == content.codes[e].barcode) {
         content.codes[e].barcode_count = parseInt(content.codes[e].barcode_count) + 1;
         found = "yes";
        }
        
       alert("Barcode: "  + content.codes[e].barcode + " | Quantity: " + content.codes[e].barcode_count); // remove - demo purposes only
    
     });
    
     if(found != "yes") {
      content.codes.push({"barcode":value,"barcode_count":"1"});
      alert("Barcode: "  + content.codes[content.codes.length-1].barcode + " | Quantity: " + content.codes[content.codes.length-1].barcode_count); // remove - demo purposes only
     }
    
    found = "no";
    
    }
    
    </script>
    
    <input id="Input"/>
    <input type="button" value="Look Up Barcode" onclick="check4code()"/>
    If I am reading your code correctly, it checks the array to see if the barcode is already in the array. I have that piece of the puzzle working.

    I guess what I am asking is that once my PHP file sends back the 1D array of the barcodes details, how would I add that information to a 2D array that holds all the barcodes, manufacturer, price and qty, whichI have scanned so far?
  6. #4
  7. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,172
    Rep Power
    184
    I guess what I am asking is that once my PHP file sends back the 1D array of the barcodes details, how would I add that information to a 2D array that holds all the barcodes, manufacturer, price and qty, whichI have scanned so far?
    That what this does:
    Code:
     if(found != "yes") {
      content.codes.push({"barcode":value,"barcode_count":"1"});
      alert("Barcode: "  + content.codes[content.codes.length-1].barcode + " | Quantity: " + content.codes[content.codes.length-1].barcode_count); // remove - demo purposes only
     }
    By the way..., are you getting back the PHP results with AJAX, because I did not see that in your code? If you want to add more variables, like: barcodes, manufacturer, price and qty; then you can add them to your JSON object (the "content" variable, in my code above) and edit the code above to update those variables, within the JSON object, as well. If you using AJAX, you will have to set-up variables to get the responseText (from your PHP) and then place those variables into the corresponding JSON object variables (ie... barcodes, manufacturer, price and qty).
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    15
    Rep Power
    0

    Javascript to PHP back to Javascript


    I am trying to figure out how to send a Javascript variable to PHP and have PHP return an array. In my javascript file, I have this:

    Code:
    //Let's check to see if UPC is already in the array
         if ($.inArray(barcode, contents) == -1){
              contents.push(barcode);
    	  $.ajax({
                   type: 'POST',
    		url: 'getBarcodeDetails.php',
    		data: 'value=barcode',
    		dataType: 'json',
    		cache: false,
    		success: function(result) {
        		     details = result;
    		     alert(details);
    		},
    	   });
    };
    getBarcodeDetails.php looks like this:

    Code:
    <?php
    	set_time_limit(0);
    	$upc = $_POST['value'];
    
    	//Database connection code
    
    	//Run a query against the database and return some results that are stored in an array called $details.
    	
    	echo json_encode($details));
    	
    	
    ?>
    The main issue, is that I can't seem to find anything that calls the getBarcodeDetails.php file. I tried placing some javascript in getBarcodeDetails.php to verify if it was being called, but it doesn't seem to be.

    I tried using this syntax in my javascript

    Code:
    $.post("getUPCDetails.php", { value: barcode });
    Not seems to trigger the PHP file.

IMN logo majestic logo threadwatch logo seochat tools logo