Jump to content

mryno

Members
  • Posts

    17
  • Joined

  • Last visited

    Never

Posts posted by mryno

  1. Is there something with what I did?

     

    Actually I think that would work. Is that the kind of solution you were looking at printf? For some reason I had it in my head that I had to foreach through the db results because a client could have:

    www.mysite.com/test1/test2/index.php - triggering two IDs and giving me issues, but that will be a problem no matter which direction I am checking from. Let's see what printf has to say though...

     

    The short answer, yeah that will work. I am going to have to find another method for checking against two identifiers in the URL since that could hypothetically, but not practically, happen.

  2. Where do these come from?

     

    $results[0][id] = 'test1'

    $results[1][id] = 'test2'

    $results[2][id] = 'test3'

     

    Where does this url come from

     

    www.mysite.com/something/test1/index.php

     

    The id's are coming from a DB query. The URL is where the HTTP request is coming from. The problem is that the URL could be anything and the identifier could be anywhere in the URL, so I can't search the same place in the URL every time because one company may do it differently from another. I need to search every element of the URL. Basically, I HAVE to use my array of id's and with each one look for a match in the URL. There are reasons. I promise. :) But I would like to have it so I don't foreach through all of them. If I call 'break' does that exit the foreach? That may be all I need.

     

    In plain English here is the problem:

    In this requesting URL there is an ID.

    Here are all the possible active IDs from the database.

    Go through the IDs looking for a match against the URL until you find a match.

    When you find a match, don't finish looking through the other IDs.

    That match is the company making the request.

     

     

    I kinda want to do a foreach/while combination.... :)

     

    While I haven't found a match, keep foreach'ing. When a match is found finish.

  3. I'm with these guys. Store the user in the session when you run the log in script. Then there is only one query and it is always available in $_SESSION for other pages. I usually store other info in the session as well like the unique key of the user and whatever other details you may need to identify him in queries down the road.

     

     

    Like The Little Guy said, if he is an admin, throw that in, too.

     

    $_SESSION['USER']['is_admin'] = true

    $_SESSION['USER']['user_id'] = '1234'

    $_SESSION['USER']['user_first'] = 'John'

    $_SESSION['USER']['user_nick'] = 'John123'

     

    That kind of stuff is always useful. You may want to break it up like I did with all your USER info in one container and so on... If it is just a simple thing that only deals with users, you don't have to.

  4. in_array is actually the opposite of what I want to do. Sorry if my explanation wasn't very good. I want to while/foreach through an array of strings and see which array item matches part of the URL.

     

    My array contains the search terms and I am searching the URL to see which of those array items it contains...if that makes sense.

  5. no, i would do it like so. it's all one statement to build the SQL, but i separated it into multiple lines so i could comment up each part. remember to read it from the middle to the top as that is how the parts will be processed. the only part you may want to tweak is step 2...not sure if you wanted the filename or just the directories in the search:

    $sql = sprintf("SELECT * FROM tableName WHERE id IN ('%s')", // 7. Puts it into the SQL statement
      implode("','", // 6. Joins them back together with quotes and commas
        array_map('mysql_real_escape_string', // 5. Runs everything through mysql_real_escape_string to be safe
          explode('/', // 4. Splits the parts on the slash
            substr( // 3. Removes the leading slash
              dirname( // 2. Strips off the file, so it's just the folder path
                parse_url($_SERVER['PHP_SELF'],PHP_URL_PATH) // 1. Gets the path of the script from the URL
              ),1
            )
          )
        )
      )
    );
    echo $sql;

     

    edit: the url http://www.mypage.com/something/test1/index.php would produce

    SELECT * FROM tableName WHERE id IN ('something','test1')

     

    Consider my world rocked!  :o

     

    Very simple and elegant. That would have taken me forever. Great code Rhodesa.

  6. I have an array of possible terms that could be in a URL:

     

    $results[0][id] = 'test1'

    $results[1][id] = 'test2'

    $results[2][id] = 'test3'

     

    Referring URL: www.mysite.com/something/test1/index.php

     

    I am looking for an alternative to foreach'ing through them and doing some kind of while so that I don't have to go through the entire array which could be quite long especially if the first term is a match...don't want to go through all the rest if I don't have to.

     

     

    **More Than You May Want To Know**

    I am basically checking the URL for known identifiers that I have stored in the DB that will identify a URL as belonging to a certain client.

     

    Given the project, this is just the way we have set up to serve multiple clients based on this specific project. There are other factors that have made it so that the URL identifies the client, but we won't go into that. :)

  7. I edited my previous post, but I didn't make it in time:

     

    The issue with doing it your way corbin is simply that who's to say that a client's url to this isn't going to be:

    www.clientsite.com/something/test1/test2/index.php

    or that one of their directory names contains the term I am searching...just too many uncontrollables, but I do agree with you in general that MySQL should be used rather than using PHP to go through records. There is always a way with MySQL! :) Some of the data I have had to deal with IS hierarchical data, which doesn't help my lack of skills...

     

    In my experience with large-scale (100's of millions) record sets, it is beyond a doubt faster to use MySQL. It probably doesn't matter (isn't noticeably faster to the end user) as much with smaller sets (100's to 1000's) of data. MySQL is still faster in the smaller sets, but what is a tenth of a second? :)

  8.  

     

    That is my thought also. I try to keep the workload on PHP. A query within a foreach just seems unnatural to me. I have used it once or twice, but there are definitely TODO comments right next to them. :)

     

    Thanks for the help!

     

    That would seem like it would kill the DB if you had a lot to loop through. I mean, I've had my share of hammering mysql so of course you can get away with

    small tables and putting queries in a loop.

     

    But it would seem to make more logical sense to get all the info out first, put it in an array, and loop through the data with PHP...

     

    Absolutely true. I am not even to beta yet, so it just has to work with a very small load now for demos, but I am optimizing what I can so when there is a load I don't have to worry about hitting the DB so often. Most of that is my lack of MySQL skills on those 4th and 5th joins.... :)

     

     

     

     

    The issue with not doing it your way corbin is simply that who's to say that a client's url to this isn't going to be:

    www.clientsite.com/something/test1/test2/index.php

    or that one of their directory names contains the term I am searching...just too many uncontrollables on my part, but I do agree with you in general that MySQL should be used rather than using PHP to go through records. There is always a way with MySQL! :) Some of the data I have had to deal with IS hierarchical data, which doesn't help my lack of skills...

  9. I would have done the latter, get all the info out of mysql and let PHP parse through it. Perhaps it's just my fear of overloading mysql too much.

    I never like mysql doing the heavy queries....

     

    In fact, you can even have mysql do the queries and parsing with LIKE functions built into mysql, but that would put more load on the DB....

     

    That is my thought also. I try to keep the workload on PHP. A query within a foreach just seems unnatural to me. I have used it once or twice, but there are definitely TODO comments right next to them. :)

     

    Good point corbin. If I could use the LIKE or something like that, it would be worth looking into. I think in this specific case since there are a bunch of unknowns and I won't have a lot of control over what people put in their URLs, I better put the query in the hands of the known variables. I agree though. Usually probably better and faster to let MySQL handle it if you can get it into a query.

     

    Thanks for the help!

  10. That's probably how I would do it to.

     

    What about the "strstr" function? It seems to provide the same kind of functionality. I don't know which one would be "faster" though.

     

    You would parse the URL? The only problem is I would have to foreach and then query right?

     

    // parse URL to get the subdirectories and put them in the array $url pieces

    foreach $urlpieces {

    // Query each piece to see if it matches any SiteIDs in the DB

    }

     

    OR

     

    // Run query to get all SiteIDs as $results (1 fast query)

    foreach $results {

    // Match against the URL as a string using strpos or something similar

    }

     

     

    Is it faster to query multiple times (probably a maximum of 5) or is it faster to query once (getting possibly 100s of records) and have PHP go through them?

  11. The part that I am looking for could appear in different places. For example:

     

    www.mysite.com/something/test1/index.php

    www.othersite.com/test1/index.php

    ...

     

    The possibilities are pretty limitless based on the client and the way they want to set up their site. I am just trying to make it as flexible as possible. Again, if you know a better way to do that or if maybe it is better to parse the URL and search on those, let me know. I am not an expert on efficiency yet. :-)

  12. I have an array of results from MySQL. I want to loop through them until I find that one of the arrays' elements match a part of the URL requesting the page.

     

    My URL is: www.mypage.com/something/test1/index.php

     

    For Example:

    $results[0][id] = 'test1'

    $results[1][id] = 'test2'

    $results[2][id] = 'test3'

     

    When I am going through the results I want to stop when I find the record that matches (in case there are a lot). Or if you know a better way, that would be cool, too. Right now I am just foreach'ing through the results and when I find a match using strpos, I set that as the one I want. I would just rather not foreach through potentially thousands of records. Thanks for the help!

     

    Ryan

×
×
  • 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.