-
Posts
6,906 -
Joined
-
Last visited
-
Days Won
99
Posts posted by ginerjm
-
-
You're worried about an insert having to update multiple indices and you have a lot of inserts going on. What about the simple fact that your script wants rebuild THE ENTIRE set of indices multiple times per day?
-
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?
-
If you have established indices that are maintained (and why wouldn't they be?) then there (As I Already Said) is no need to re-create them. When a table is updated, so is its index. Simple Database Knowledge.
-
Somewhere in your code you are probably in a loop creating new vars or arrays and trying to allocate too much memory.
PS - I don't understand why ANYTHING you do to your data tables would necessitate having to rebuild ANY index.
-
Thanks again. Case Closed.
-
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? -
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?
-
So - how would you suggest I get the php data into a js format and then how do I search the js formatted data?
-
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.
-
This code:
$i = 0; while ($i < $AnswerID) { $AnswerID= $_POST['AnswerID'][$i]; ... ...
has got to be part of the problem. How can you use a certain var as a control for your loop and then immediately change its value inside that loop?
-
Why would one add an index everytime a script is run? If you WANT an index, then create it by hand and stop worrying about it.
As for other problems - turn on php error checking, add your own error checking (like test if your query runs before moving on) and see what pops up.
-
First thing would be to rewrite using mysqlI or PDO interface. Once again (for the benefit of all) MYSQL is deprecated and has been for years. That means Stop Using It.
-
Again - which line is the error message pointing at?
Also - if you only expect a value of 1 or 2 from the checkboxes, why aren't you using a radio button instead?
-
Huh?
What line is the error on? Read up on how checkboxes are handled - you don't get a $_POST element for a checkbox if it is not checked, so you will get a warning/notice because of that. Do you have error checking turned on?
-
Funny post. You show us one set of data and then show us code that doesn't match it. Confusing.
-
Do you get an error message? You do realize that a checkbox doesn't return a value unless it is checked? You really need to validate your input vars and NOT use them directly in your query statement to avoid injection. Also might help to see your html.
-
Aha! So it's not an unknown format. Leave it to the Gurus to recognize that! Kudos Jacques1 - AGAIN!
-
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.
-
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.
-
First thing I would do is consolidate your tables into one db. Why have 3 databases for common data?
-
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.
-
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.
-
Be sure to read and follow Jacques advice - I should have done what he did. Good practice makes for safe code.
-
Try
foreach ($songStmt as $song) echo $song['artist'] . " - " . $song['title'] . "<br>";
Much better looking output.
how to use php associative array in JS
in Javascript Help
Posted
Never mind. I found out how to dislay my update object.
After adding a new element to the object(array) via obj[key] = "value" I simply then assign the obj to my div innerHTML using JSON.stringify(obj). Voila!