I am using codeigniter with php 5.6 and mysql db for this specific purpose. I want to store strings from different languages to database and retrieve them later.
Problem I am facing here is when I am trying to grab the value, instead of giving out exact text, it shows something in the following format.
%E0%A4%A8%E0%A4%AE%E0%A4%B8%E0%A5%8D%E0%A4%A4%E0%A5%87
Here's some test code
function charset($get = NULL){
$direct = "नमस्ते";
$this->load->database();
if($get != NULL){
$this->db->insert('test', array("name"=>"get", "value"=>$get));
$this->db->insert('test', array("name"=>"direct", "value"=>$direct));
}
$results = $this->db->get('test')->result_array();
echo "<pre>";
print_r($results);
}
and Here are the results
Array
(
[0] => Array
(
[id] => 16
[name] => get
[value] => %E0%A4%A8%E0%A4%AE%E0%A4%B8%E0%A5%8D%E0%A4%A4%E0%A5%87
)
[1] => Array
(
[id] => 17
[name] => direct
[value] => नमस्ते
)
)
how would you get (or convert) the exact word instead of ut8 encoded string?
I have checked your code like below and it is returning value proper:
$variable = '%E0%A4%A8%E0%A4%AE%E0%A4%B8%E0%A5%8D%E0%A4%A4%E0%A5%87';
echo urldecode($variable);
Hope I understood your code properly.
There are multiple things at play here
MySQL
In MySQL you need to configure the connection/client to use it:
SET NAMES 'utf8'
In PHP it might be something similar to this:
$this->db->query("SET NAMES 'utf8'");
HTML
When outputting to HTML, you'll also have to tell the client (browser) what data you're passing:
header('Content-Type: text/html; charset=utf-8');
And/or in your HTML body:
<html>
<head>
<meta charset="utf-8" />
Related
I'm working on a script that retrieves values from a MySQL database for each individual letter within a $word. The script works great, but not for Latin characters like an enye.
Example Code:
$word = "Cañones";
$letters = str_split($word);
print_r($letters);
$query = "SELECT GROUP_CONCAT(Alpha) AS Alpha,
GROUP_CONCAT(Letter_Box_Width) AS Letter_Box_Width
FROM Font_Krinkes
WHERE Alpha IN ('" . implode("','", $letters) . "')";
Output of print_r
Array
(
[0] => C
[1] => a
[2] => ?
[3] => ?
[4] => o
[5] => n
[6] => e
[7] => s
)
PHP Notice: Undefined index: ?
PHP Notice: Undefined index: ?
It seems to me that the enye is being stored and retrieved from my $letters array incorrectly. It's being retrieved as a question mark... Then, of course, the query for the question mark value fails, and this is why I'm getting the Undefined index error. How do I deal with this enye character within my array?
After working through the comments, I realize my code has more issues than just the mb_str_split issue. I'm still unable to query the database correctly.
Any help is much appreciated.
In storing ang getting string data from your database and making sure all characters display well, you need to set your connection's character encoding. Simply add this line:
mysqli_set_charset($conn,"utf8"); wherein your $conn is your connection to the database.
I don't know if this is even called "hardcoding", but I would like to build an array (for example from a mysqli_fetch_assoc query) and output it for reuse in a config.php file.
So this output from mysql would be:
[structure] => Array
(
[0] => Array
(
[structureid] => 23
[active] => 1
)
[1] => Array
(
[structureid] => 25
[active] => 1
)
And it would be stored like this in a config.php file:
$structure[0]['structureid'] = 23;
$structure[0]['active'] = 1;
$structure[1]['structureid'] = 25;
$structure[1]['active'] = 1;
Is there an easy and quick way to accomplish this?
As someone has pointed out, I could resolve this with using a json file - which I'm already doing - but I wanted to check if there would be another (quick) way of doing it as described above.
You can use var_export(), to create a readable (and in-code-reusable) array, that you can then put in a config file:
$reusableCode = var_export($yourArray, true);
Or to just output it:
var_export($yourArray);
You can put that in a config file with an return, like this:
return array(
'your' => 'values'
);
and then require it from your code:
$configData = require('yourConfigFileWithTheArray.php');
I Initially misread your question,
So you want to store mysql resource to a file?
You mentionned that you are using JSON is to solve this.
This is a good way to do it, the other way would be to serialize/deserialize the object than save them to a file. JSON solution is simple human readable.
Example:
$data = serialize($rows);
file_put_contents('data.txt', $data);
$data = file_get_contents('data.txt');
$rows = unserialize($data);
But you won't be able to read the output of the serialization.
while I was searching through some file in a php library, i found some documents like this
a:1:{i:0;a:1:{s:3:"cnt";s:1:"1";}}
This is definitely not JSON. Does any one know what is this? or is it a custom syntax for the guy who wrote the library ?
That is a serialized data and not any programming language syntax.
For your understanding...
<?php
$arr = ['a'=>1,'b'=>2,'cnt'=>5];
echo serialize($arr);
OUTPUT :
a:3:{s:1:"a";i:1;s:1:"b";i:2;s:3:"cnt";i:5;}
Language in Php more info on this link unserialize
This is basically a serailize form of array
of follwoing array
Array
(
[0] => Array
(
[cnt] => 1
)
)
You can get it back by into array
$a = 'a:1:{i:0;a:1:{s:3:"cnt";s:1:"1";}}';
$unserailize_a = (unserialize($a));
To convert an array into string us the Serialize
I'm trying to work in a CodeIgniter environment and while trying to collect and gather some information into variables I'm getting some PHP NOTICE errors that don't seem right.
Here's the chunk of code where the error(s) occur:
if (empty($events['user'][$user_id])) {
unset($events['user'][$user_id]);
} else {
foreach ($events['user'][$user_id] as $event) {
$events['user'][$user_id]['events']['event_id'] = $event_id = $event['event_id'];
$events['user'][$user_id]['event']['date'] = $this->events_model->getEventDates($event_id);
$events['user'][$user_id]['event']['date'] = $events['user'][$user_id]['event']['date'][0]['date'];
$events['user'][$user_id]['event']['request_title'] = $event['request_title'];
$events['user'][$user_id]['event']['event_status_text'][] = $this->events_model->getEventStatusFromSectionStatuses($event_id);
$request_data = $this->requests_model->getRequestInfo($event['request_id']);
$events['user'][$user_id]['event']['ministry'] = $this->ministries_model->getMinistryTitle($request_data[0]['requesting_ministry']);
// more stuff will go here...
}
$content_data['event_status_text'] = $events['user'][$user_id]['event_status_text'];
$content_data['events'] = $events['user'][$user_id]['complete_events'];
$content_data['totals'] = $events['user'][$user_id]['totals'];
$content_data['updated_events'] = $events['user'][$user_id]['updated_events'];
}
The specific line of the first error is the third line inside the foreach loop that ends with ['date'][0]['date']. It's the [0] that PHP is telling me is undefined. However, if I echo that exact same variable like this:
echo $events['user'][$user_id]['event']['date'][0]['date'];
...it outputs a value as would be expected, which also tells me that the [0] is NOT undefined after all. I'm not actually changing the variable. The only difference is that I'm echoing it instead of assigning it to another variable.
If I use # to ignore it in here, it happens again a few lines later on the line ending with getMinistryTitle($request_data[0]['requesting_ministry']).
Can you see what I'm doing wrong? Let me know if you need to see more of the code.
Here's the getEventDates() code as requested (note this is not my code):
function getEventDates($event_id)
{
$sql = "SELECT date FROM `event_dates` WHERE event_id=? ORDER BY date";
$res = $this->db->query($sql, array($event_id));
return $res->result_array();
}
if I print out $this->events_model->getEventDates($event_id) I get the following:
Array
(
[0] => Array
(
[date] => 2014-05-01
)
[1] => Array
(
[date] => 2014-05-08
)
[2] => Array
(
[date] => 2014-05-15
)
[3] => Array
(
[date] => 2014-05-22
)
[4] => Array
(
[date] => 2014-05-29
)
[5] => Array
(
[date] => 2014-06-05
)
[6] => Array
(
[date] => 2014-06-12
)
)
Hmmm... is it possible that this error is happening because there isn't a direct value contained in [0], but rather another array level? Please note that I did not structure this output. Someone else coded this and it's just my job to come in and work with it.
Without seeing the rest of your code this is confusing:
$events['user'][$user_id]['event']['date'] = $this->events_model->getEventDates($event_id);
$events['user'][$user_id]['event']['date'] = $events['user'][$user_id]['event']['date'][0]['date'];
Why would you be setting $events['user'][$user_id]['event']['date'] in one line and then in the next overriding it again?
My best advice would be to set the first assignment in a variable independent of the array, and then calling that variable for the data:
$event_dates_temp = $this->events_model->getEventDates($event_id);
$events['user'][$user_id]['event']['date'] = $event_dates_temp[0];
And perhaps adding a conditional check to ensure you are setting things that exist:
$event_dates_temp = $this->events_model->getEventDates($event_id);
if (array_key_exists(0, $event_dates_temp)) {
$events['user'][$user_id]['event']['date'] = $event_dates_temp[0];
}
Also, it’s unclear at what point you are doing this:
echo $events['user'][$user_id]['event']['date'][0]['date'];
And what is the output when you do dump like this:
echo '<pre>';
print_r($events['user'][$user_id]['event']['date']);
echo '</pre>';
It happens when you are trying to access a value which is not set for that index, please read this link for more information.
There are two options available:
Ignore these notices by telling the PHP error_reporting to not show notices error_reporting(E_ALL ^ E_NOTICE); but it is a good practice to fix this error by adding a check if a value exists.
Fix the values by isset function to see if the value/index contains any data.
If You hide notice set error_reporting(E_ALL ^ E_NOTICE);
Solution is use isset() to solved this issue.
I need to do the following for my website.
$comment = "[item]Infinity Edge[/item]<br>[item]Eggnog Health Potion[/item]";
$this->site->bbcode->postBBCode($comment);
The BBCode function is like this:
function postBBCode($string)
{
$string = nl2br($string);
$string = strip_tags($string, '<br></br>');
$string = $this->tagItem($string);
return $string;
}
function tagItem($string)
{
//Get all values between [item] and [/item] values
//Appoint them to an array.
//foreach item_name in array, call convertItems($item_name) function.
//Now, each item_name in array will be replaced with whatever convertItems($item_name) function returns.
//return modified string
}
function convertItems($itemName)
{
// -- I already made this function, but let me explain what it does.
//Query the database with $itemName.
//Get item_image from database.
//Return '<img src="$row['item_image']></img>';
}
Okay, I already asked my questions between functions. I hope you understood what I am trying to do.
Basically, anything between [item] and [/item] tags will be converted into an image, but the image path of each items will be taken from database.
The part I am having hard times is getting values between [item] and [/item] tags correctly. It should be getting all the correct matches it finds, not the first match.
If you use a preg_match_all on $string, you'll get a result set with all the matches:
$results = array();
preg_match_all('#\[item\](.*?)\[\/item\]#', $string, $results);
$results will have an array of results that looks like this:
Array
(
[0] => Array
(
[0] => [item]Infinity Edge[/item]
[1] => [item]Eggnog Health Potion[/item]
)
[1] => Array
(
[0] => Infinity Edge
[1] => Eggnog Health Potion
)
)
Now you should be able to loop through the $results[1] and send it through convertItems.