Jump to content

is_numeric...is_string...on array


jimfog

Recommended Posts

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

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

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

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

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

 

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 by jimfog
Link to comment
Share on other sites

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

Οκ...Ι 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

 

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

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

  • 2 weeks later...

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...