Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP Getting the ID in the DB by comparing the current URL
    text
    copied!<p>I am building a visitor tracking system and I want it to be as much as abstract as possible, without having to put static variables inside the code. The campaign table is structure this way:</p> <pre><code>ID - NAME - URL - REDIRECT URL </code></pre> <p>Where ID is the <code>ID</code> of the campaign ( auto-incremented ), <code>NAME</code> is the name of the campaign, <code>URL</code> is the url of the campaign. The fact is that I should get the ID of the campaign based on the campaign we actually are and I am doing it by comparing the current URL with the URL in the DB. By doing this i get the campaign ID and do the rest of the work. The problem is that with my function I correctly get the URL of the campaign but when comparing it with the URLs in the DB not always it returns what expected. I tried to get the URL with all the possible variates but sometimes the URL does not return in the right way and the ID is not recognized in the DB. How may I solve this issue? Is there another logic I may use instead of comparing the URL?</p> <pre><code>$campaign = getCampaignDetails("url", cleanUrl($_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'])); function cleanUrl($url) { $url = str_ireplace("http://www.", "", $url); $url = str_ireplace("http://", "", $url); $url = str_ireplace("www.", "", $url); $url = str_ireplace(".php", "", $url); $url = str_ireplace("https://www.", "", $url); $url = str_ireplace("https://", "", $url); return $url; } function getCampaignDetails($table, $var) { $query = "SELECT * FROM _campains WHERE ".$table." = '$var'"; $result = mysql_query($query) or die("Getting campaign details via url failed: " . mysql_error()); while ($row = mysql_fetch_assoc($result)) { return $row; } } </code></pre> <p>Example of values of a campaign in the DB:</p> <pre><code>ID = 1, Name = tgcampaign, URL = site.com/campains/tgadv </code></pre> <p>For now to avoid unexpected errors I am just passing the ID directly in the file, but this is something I would like to avoid, like below:</p> <pre><code>$campaignId = "1"; $campaign = getCampaignDetails("id", $campaignId); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload