jimfog Posted February 16, 2015 Share Posted February 16, 2015 I have an array with seven members and I want to test some of them if them if they are string and some if they are numeric. What would be the most efficient way to do this you think... In the PHP manual a reference is made to a foreach loop,nonetheless this is a good solution only if we are testing for is_numeric only for example. Since we are dealing here with two tests,what is the way to go you think? Link to comment Share on other sites More sharing options...
justsomeguy Posted February 16, 2015 Share Posted February 16, 2015 You would still loop through it. You could also use something like array_filter or array_map. Link to comment Share on other sites More sharing options...
Ingolme Posted February 16, 2015 Share Posted February 16, 2015 The question is: How do you know which ones you're testing as numeric and which ones you're testing as a string? Link to comment Share on other sites More sharing options...
jimfog Posted February 16, 2015 Author Share Posted February 16, 2015 The question is: How do you know which ones you're testing as numeric and which ones you're testing as a string? I know that from before... The app I am building sends a JSON to the server...the keys are fixed each time..the values change of course. And of course I know which data type every key is. Link to comment Share on other sites More sharing options...
Ingolme Posted February 16, 2015 Share Posted February 16, 2015 If there are only seven and you already know the index then you don't need a loop. The code would be something similar to this: if(is_numeric($arr[0])) { ... }if(is_numeric($arr[1])) { ... }if(is_string($arr[2])) { ... }if(is_numeric($arr[3])) { ... }if(is_string($arr[4])) { ... } It's more efficient than a loop. Loops in general are less efficient than a long list of instructions, programmers often use a technique called "loop unrolling" to reduce the number of loops or the number of times a loop has to execute. Link to comment Share on other sites More sharing options...
jimfog Posted February 17, 2015 Author Share Posted February 17, 2015 since this question has to do with validation I want to make one last important question. The code of course in this topic relates to server-side. My question concerns the reply from the server...as I see it...it must be using both json_encode(in case js validation failed) AND print/echo statements in case js is disabled. Tell me if I am correct with the above? Link to comment Share on other sites More sharing options...
Ingolme Posted February 17, 2015 Share Posted February 17, 2015 If this page is being used in response to an AJAX request, then output a JSON string. If it's handling the forum submission on its own then you'll have to output an HTML page. If you want to have the same page to do both, just add a parameter to the query string when you make the request using AJAX so that PHP can know what type of response to give. In your Javascript, add ?ajax=1 to the URL, then PHP can test for that: if(isset($_GET['ajax'])) { // Show JSON} else { // Show HTML} Link to comment Share on other sites More sharing options...
jimfog Posted February 17, 2015 Author Share Posted February 17, 2015 (edited) If this page is being used in response to an AJAX request, then output a JSON string. If it's handling the forum submission on its own then you'll have to output an HTML page. If you want to have the same page to do both, just add a parameter to the query string when you make the request using AJAX so that PHP can know what type of response to give. In your Javascript, add ?ajax=1 to the URL, then PHP can test for that: if(isset($_GET['ajax'])) { // Show JSON} else { // Show HTML} I must make some clarification first...the request are made by backbone(which uses ajax of course for the requests). So...in a backbone collection I have assigned an URL property which points to a page in the server...like that: var Events = Backbone.Collection.extend({ model: BBEvent, url: 'events.php', }); I am assuming I am going to add ?ajax=1 above...in the url property...correct? Ok this is the way PHP understands that the request is made with JS. How it is going to understand though that the request is NOT made by js....I am confused regarding this. How am I going to ensure that the request is not made by JS.... You claim that this happens by the identification of the GET ajax parameter.... How am I going to ensure that this is absent from the URL so that this is recognized by PHP an non sent by JS request? There is something missing here... Edited February 17, 2015 by jimfog Link to comment Share on other sites More sharing options...
justsomeguy Posted February 17, 2015 Share Posted February 17, 2015 There is something missing here...There definitely is. What are you missing? If the URL contains a parameter called ajax to tell PHP that the request came from ajax, and you're trying to figure out how to tell if it didn't come from ajax, the answer is blatantly obvious and staring you right in the face. What are you missing?I'll spell it out for you in case you're confused: if the presence of a URL parameter indicates that the request DID come from ajax, then wouldn't it make sense that the lack of that same URL parameter indicates that the request did NOT come from ajax? I mean, this is a true/false situation here, there aren't more than 2 possible states. Either it came from ajax, or it didn't. If you have a URL parameter which indicates that it did come from ajax, and that parameter is missing, wouldn't it make sense that the request did not come from ajax? I don't understand why you're confused by that. Link to comment Share on other sites More sharing options...
jimfog Posted February 19, 2015 Author Share Posted February 19, 2015 Οκ...Ι understand but I need to say that maybe this question is in vein. My app cannot work with javascript...and the submissions which are made by ajax..are made by pressing the submit button on a dialog box created by jquery UI. So on this case,js is the only alternative. The way the app is made I cannot make it to run with PHP only. Link to comment Share on other sites More sharing options...
justsomeguy Posted February 19, 2015 Share Posted February 19, 2015 My app cannot work with javascriptWhat do you mean? jQuery and ajax are Javascript, you're obviously using that. I'm not sure what the problem is that you're trying to solve. Link to comment Share on other sites More sharing options...
thescientist Posted February 19, 2015 Share Posted February 19, 2015 My app cannot work with javascript...and the submissions which are made by ajax..are made by pressing the submit button on a dialog box created by jquery UI. What do you mean? jQuery and ajax are Javascript, you're obviously using that. I'm not sure what the problem is that you're trying to solve. I'm confused too, because immediately afterwards this is said So on this case,js is the only alternative. The way the app is made I cannot make it to run with PHP only. Link to comment Share on other sites More sharing options...
jimfog Posted February 19, 2015 Author Share Posted February 19, 2015 Yes you are right...I misled you...my app cannot work without JS. I do not intend to make the app work with PHP Link to comment Share on other sites More sharing options...
justsomeguy Posted February 19, 2015 Share Posted February 19, 2015 How am I going to ensure that this is absent from the URL so that this is recognized by PHP an non sent by JS request?So you need to make sure PHP recognizes something, but you're not using PHP for this application? Link to comment Share on other sites More sharing options...
sandeepm Posted March 2, 2015 Share Posted March 2, 2015 is_numeric($string) by using this we can find the string contain string or numeric Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now