Caching Values of MySQLin Browser - sql

I am creating a website in which user enters his position(which can only be the ones stored in my database). To make it user friendly, I want to show him the available choices of positions as he types the positions. Is there a way to cache all the values in SQL(around 30 to 40) in browser beforehand, so that user can see them while typing.

You can query your data base through PHP and most other server side scripts and call them through ajax. You can also use pure javascript with the new html5 specs (but they are only supported in webKit browsers at the moment).
If you want to do it without ajax, you can get the values when the user accesses the page and put them in a javascript array. This way when the computer reads the javascript the custom values are already there. The problem with this meathod is it will significantly slow your websites response time.
Example: (php/pesudocode)
<?php
$options = $mysql->getOptions(); // This is the pesudo-code bit.
echo 'options = [';
foreach ( $opt in $options )
{
echo '"' . $opt.name . '"' . ',';
}
echo '];"
?>
This gives something like
options = [ "opt1", "opt2", "opt3", ];
which should be read as an array in javascript. The only problem is the extra comma but you should be able to solve that easy enough. (and firefox didn't even give a warning)

Related

How to rewrite rules NginX in this example?

I am creating an API to feed some apps.
So the app could call these possible URLs to get information from the database;
mysite.com/api/v1/get/menus/list_tblname1.json.php
mysite.com/api/v1/get/menus/list_tblname1.json.php?type=arr
mysite.com/api/v1/get/menus/list_tblname2.json.php
mysite.com/api/v1/get/menus/list_tblname2.json.php?type=arr
In php I have already the code that grabs the tblname from the URL and give me back all the table content. It works good (it is not the final version).
But now I find myself copying and pasting the same code for each page where the URL points to. Here is the code:
<?php
header('Content-Type:application/json');
include_once '../../../../class/db.php';
$verb=$_SERVER['REQUEST_METHOD'];
$filePath=$_SERVER['SCRIPT_NAME'];
$split1 = explode("/", $filePath);
preg_match("/(?<=_)[^.]+/", $split1[5], $matches);
$tableName = $matches[0];
if ($verb=="GET") {
header("HTTP/1.1 200 ok");
if(isset($_GET['type']) && $_GET['type']=="arr"){
echo db::get_list($tableName,'arr');//Reply ARRAY
}
else{
echo db::get_list($tableName);//Reply JSON
}
}
else{
die("Nothing for you at this page!");
}
I mean, I have the same code inside each these pages.
list_tblname1.json.php
list_tblname2.json.php
I am not sure how to solve this situation but I think that this is case for
rewrite rules.
So, I think a possible solution is to create one page that could call
returncontent.php for example and create rules in the server that should point to the same page when certanlly pages are requested and pass the parameter $tableName to the page. I think I should pass the regex to my server and grab the $tableName with $_GET[] (I think) inside returncontent.php.
I am not sure about it.
I am using NginX.
How to implement it in this scenario?
As a rule, it's bad practice to parse a URI in NginX and pass the result downstream.
Rather: mysite.com/api/v1/get/menus/returncontent.php?file=list_tblname2.json
No changes to NginX needed. Parse the query param (file) in PHP.

How to generate username of specific pattern in drupal during registration

I want to generate a usernames st + first two initials of their first name + their last name.
There are few modules to create username of specific pattern but none of them can accomplish my requirement. I am not Drupal module expert.
What you can do is to install the automatic username module:
Navigate to the configuration page of this module: admin/config/people/accounts/patterns
Then, in the "Other settings" section you can check the option "Evaluate PHP in pattern."
This allows you to execute PHP in the "Pattern for username" field. Two things to consider:
PHP is executed after the token replacement, this allows you to actually manipulate the strings.
You need to echo the final result.
Make sure that the php is wrapped with <?php ?> tags. Also, if you make a syntax error, it will only be seen upon registration (Or whenever the script is executed.)
Now let's say you configured your users to have a first_name field and a last_name field (with those specific field names)
Your PHP could look something like this:
<?php
$initials = substr("[user:field-first-name]", 0, 2);
echo 'str' . $initials . "[user:field-last-name]";
?>
First, Note that the token are between double quotes to make sure that what is outputted by the token is considered a string by PHP
Second, the php function substr is used to get the 2 first letters of the first name.
Now, things I don't know
I have no idea what happens if two users happen to have the same resulting username
I don't know what happen if a user enters a double quote in his name, might be dangerous, you might want to escape it with a str_replace or something like that

Trouble inserting fbml into database

Hi I am working with the facebook javascript sdk and I am having succcess using fbml to list the logged in users name like so...
<fb:name uid="loggedinuser" useyou="false"></fb:name>
What I would like to do next is assign this value to a php variable and insert it into my database. The problem I'm having is when I assign this value to a php variable like so...
$first_name = ('<fb:name uid="loggedinuser" useyou="false"></fb:name>');
What gets inserted into the data base is the literal expression of
<fb:name uid="loggedinuser" useyou="false"></fb:name>
instead of the users real name. Is there anyway to do this with the javascript sdk? Thanks!
You need to get the variable of
<fb:name uid="loggedinuser" useyou="false"></fb:name>
before assigning it to
$first_name
FBML data is generated on the fly, there isn't a way to transfer data over like this since FBML would load later after PHP code is completed. In addition
$first_name = ('<fb:name uid="loggedinuser" useyou="false"></fb:name>');
Would never evaluate to anything other than a string, this is just how PHP works. This one line of code cannot possibly determine the nature within based on a string.
The JavaScript SDK offers a way to do this already
FB.api('/me', function(response) {
alert('Your name is ' + response.name);
});
Though this in JavaScript so you need to send the data in a AJAX call to your PHP application. It would be much easier if you just retrieved the session information after JS SDK login and let the PHP SDK take care of the rest.

Retrieving sql data in iframes

I have a php script (ini.php), that open in an iframe inside of "main.php":
<iframe src='ini.php' style='width:650px;' frameborder='0' id="IDMain" allowtransparency="YES" scrolling="NO"></iframe>
The SQL does not retrieve the data in "ini.php".
But if I incorporate the code of "ini.php" in "main.php", without using an iframe, the query retrieves all the information.
I must have an iframe in order to change all the different scripts in the same iframe.
Any help here?
Thanks
In addition more information:
The main.php is in session after logged. Previously I had the whole frame repeated in each script, and everything was ok.main.php, ini.php, and so on. Each script with header, left, right, main and footer. To make it faster and easier I decide to make the main script called main.php, that with an included script called config.inc.php that has several functions, also connects to data base, to retrieve some data to the header, left , right and footer sides of the main area (iframe) where the others different scripts, just to opens inside of a iframe, maintaining the information around. Each script, (like ini.php), that runs in the iframe also include the config.inc.php file, to connect as well to database. But does not retrieve any information
The connection to database in config script is:
<$connect=mysql_connect ("localhost","database","password") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("database", $connect) or die ("'I cannot connect to the database "); >
The query in ini,php (and others) is:
`
if ($id) {
$id_session=$_SESSION['id_session'];
$namesession=$_SESSION['name_session'];
$sql="select * from data where id='$id_sessino' limit 1 ";
$result=mysql_db_query("database",$sql);
$regist=mysql_fetch_array($result);
$id=$regist["id"];
$id_session=$regist["id"];
$namesession=$regist["name_session"];
$country=$regist["country"];
And soo on…
}
`
I hope with is information it makes it clear what do I mean. Thank you
Check PHP help for mysql_db_query. Since you are not giving it any open db link, it tries to do some default magic, which likely fails, because, as I mentioned in the comment, the two scripts (in parent window and inside the iframe) are run separately and have no idea about each other *unless you are doing some synchronizing, that isn't shown in the code snippets. That means that the iframe script doesn't know anything about the connection you've opened in the parent page, and fails. Check your logs, you should see some E_WARNING complaints (if you are logging this level).
Also, as the documentation says, you might be better off with MySQLi or PDO_MySQL extensions. But the principle will remain the same - the script in questoin has to have an open connection to the database (and I'm not sure whether you can easily and safely transfer an open from one script to the other one, so you'll likely have to do it all in the iframe script).
Try two things, first I would change the MySQL_fetch_array part to
While($regist = mysql_fetch_array($result))
{
//Code goes here
}
Secondly try referring to the array vales as numbers rather than names, so regist['id'] would become regist[0]. They will be in the order the are in the database.
Also since you select your database when creating connection why not just use mysql_query("QUERY HERE"); Rather than mysql_db_query

WHMCS Addon Module with Multiple Pages

I am currently working on developing my first WHMCS Addon Module, and so far everything has gone very well. However, I need to make multiple content pages, and the only way to display output according to the Wiki Article is to echo it in the output function. How can I create individual pages when the only way to display content is via a single PHP function?
I am assuming using divs, and hiding the relevant divs, although not exactly the best method. It says you can use the "modulelink" variable to link back to the module, but I have no idea how to use this, or if it can be used for making multiple content pages.
http://docs.whmcs.com/Addon_Module_Developer_Docs
After some tinkering, it was much more simple than I realized, and the "modulelink" variable was just so you could link back to the page. To create additional pages, you can basically just do something along the lines of...
Datacenters
Then in the output function have...
$category = $_GET['catid'];
if ($category == "1" || $category == "")
{
//page 1 content here
}
else if ($category == "2")
{
//page 2 content here
}
and so on.