Jump to content

ginerjm

Members
  • Posts

    6,906
  • Joined

  • Last visited

  • Days Won

    99

Posts posted by ginerjm

  1. I know I marked this as 'Answered', but I have a followup question.

     

    My script receives user input that my js code adds to the object that my json code created. For debugging purposes I have made the div that holds the php array visible so I can see what my json object contains. I have managed to get the js code to add a new element to the object which I verify with the hasOwnProperty after adding it. What I can't seem to do is get the div to be refreshed with the updated contents of the object. I try to set the value or innerHTML props of the div but all it displays "[object Object]".

     

    Can I not do this?

  2. Solved! The code that I had to do a search was outside a function and therefore executed out of sequence.

     

    Thank you very much Jacques! Once again..... well, you know.

     

     

    OOPS - last question. Here is my search function:

     

    	function keyExists(key, search)
    	{
    		for (k in search)
    		{
    			alert("checking "+k+" - "+search[k]);
    			if (k === key)
    			{
    				return true;
    			}
    		}
    		return false;
    
    For my test I use the Bluman code - 055 - which I can see on screen as being the first element of the array/object yet I watch search function begin with the 2nd element and finally get to the first one LAST! What's that about?
  3. Well, I thought I pieced it together but no luck.

     

    Before I begin outputting any of the html/JS code I do this:

    $json = json_encode($_SESSION['atts_ar']); // encode my array
    $json_object = htmlspecialchars($json,ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML5, 'UTF-8');
    

     

    Then I created a JS function:

    function getJsonData()
    {
       atts = JSON.parse(document.getElementById('json_data').innerHTML);
       alert("atts is "+atts);
    }
    
    which gets output as part of my web page.

     

    At the bottom of my html/web page I output this:

    echo "<div id='json_data' class='json_data'>";
    echo $json_object;
    echo "</div>";
    

    where the class is defined as visible so I can see the data which looks like this:

     

    {"055":"Bluman, Anthony","148":"Duncan, Paul",.....

     

    Then in my body tag I added an onload to call the JS function:

    <body onload='getJsonData()
    So to summarize:

    I created the proper json object;

    I buried it in my html

    I wrote a JS function to grab the buried data

    I called the function on the page load.

    Net result is that the alert shows that the end result is an object

     

    Is this what I should expect? And if so, how do I search this array/object based on the key elements?

  4. I have taken an PHP array that looks like this:

     

    $arr = ('key1'=>'value1','key2'=>'value2','key3'=>'value3');

     

    and run it thru json_encode() to assign it to a JS var:

     

    <script type='text/javascript'>

    var ar = <?php echo json_encode($arr, JSON_PRETTY_PRINT); ?>;

     

     

    Now I am trying to figure out how to use this JS array (object?) variable and find values based on the keys in it. Basically if given a key, I want to find out if it exists in the array and what the value of that key is. Not having much luck.

  5. 1 - the sample you provided seems to be readable but when I read into deeper I sense that your sample is not a good one since there is a consistency problem. For ex., the 'a' element seems to indicate an array but there is no closing brace

    2 - What is your table layout - just one field?

    3 - You want to take data from this complex format and put it into a similary non-dataprocessing-friendly format? Why?

     

    IMHO - the best approach would be to convert this table into a proper database format (looks like it might need at least 3 tables from your sample) and then you would have a useful data source. Which - if you really have to - would be easier to dump to excel.

  6. Bravo Jacques1!!

     

    I wish more of us here on this forum felt the same as Jacques when answering obvious student project requests for help.

     

    OP - You are at university to learn and are probably paying good money to do so. So LEARN! As Jacques says - when you have something YOU have written and need help with some portion of it, then ask here. We are not un-paid professors, but we can be free tutors.

  7. You're storing the body element of an email in your table without trying to identify the individual parts of that email first? How do you know the 'body' is actually in the format you expect it to be in? Is this email a free-form entry from some user? Do you send back a confirmation email that lets the user know that what they think they sent you is actually what you interpreted it as? And - when you store this email, how do you identify it? You have all the content in one field - that means no keys, no dates, no tracking of any kind. Kind of defeats the purpose of maintaining a database, don't you think?

     

    If it were me and I absolutely had to obtain data this way, I would try to extract the data from the email and store it as a temporary record in a well-designed table with a reference key. I would then send the collected data items along with the reference key as a hidden field back to the user and ask for a reply that confirms their submission was correct. When that email comes back to a "special" address I would have another script designed to be triggered by emails coming in to it that would look for the reference key, locate the 'temporary' record and re-key it with the user's name (?) as the key and consider this entry as a valid one.

     

    Your current method of obtaining this data is flawed. GIGO - remember that? No self-respecting data processer would ever do what you are doing.

  8. Have you tried doing any reading on how to program in php? You aren't understanding what you have been given.

     

    First - did you create the function as Jacques showed you?

     

    Second - you use that function for EACH item that you want to output.

     

    Third - you can't just add an echo since the foreach will only loop thru the single line I gave you. You need to enclose all lines to be executed by the foreach in curly braces.

     

    A little light reading of a php source is what you really need to do.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.