Creating named session array from mysql fetch array - php

I have a database with a table called translations. It has several columns, but I am trying to create a session array to house a variale:translations named array.
I've tried the below code, but I must be missing something... my end goal is to populate much of the static verbiage of a website using $_SESSION['TRANSLATIONS']['Userboards'] for example to populate the names of otherwise static content into any language supported based on the database.
$querylang = "SELECT variable,translation FROM translations left outer join languages on languages.abbrev = translations.fkabbrev WHERE languages.abbrev = 'EN'";
$sqllang = mysql_query($querylang);
while($reslang = mysql_fetch_array($sqllang)){
$_SESSION['TRANSLATIONS'][$reslang['variable']] = $reslang['translation'];
};

The above code is actually correct. The reason it didn't work was a separate issue. Thanks to #Epodax for reminding me to check the errorlog!

Related

PHP - How to globalize CRUD classes for different pages?

Kind note: Practicing PHP
I got the CRUD library from this repo: https://github.com/rorystandley/mysqli-crud-php-oop.
I setup the database, connected to to it and everything is great. I am only unaware of how to use this globally?
I have different pages for different data tables, I see it not logical to create multi "insert.php" files for each page.
Insert.php:
<?php
include('class/mysql_crud.php'); // Contains the server info (locahost, user, pass, table)
$db = new Database();
$db->connect();
$data = $db->escapeString("name5#email.com"); // Escape any input before insert
$db->insert('CRUDClass',array('name'=>'Name 5','email'=>$data)); // Table name, column names and respective values
$res = $db->getResult();
print_r($res);
I believe include(); won't solve it as each page has different data to pass around.
Should I create a condition for "if that page, then pass this data"??
Thanks in advance.

Accessing releated field

In FileMaker I have (a.o.) two related tables, where Betreuer_id of table "studentpaper" refers to id of table "Betreuer".
With help of FileMaker's PHP API, I want to access a record in "studentpaper" including the releated fields. Howerver, the later poses a problem. Consider the following PHP code:
$findCommand =& $fm->newFindAllCommand('studentpaper');
$result = $findCommand->execute();
$records = $result->getRecords();
$record = $records[0];
echo $record->getField('Titel'); // okay
echo $record->getField('Betreuer_id'); // okay
echo $record->getField('Betreuer::Name');
// ERROR: get empty string, even related record has a non-empty field
I have expected for "Betreuer::Name" the correct related result, as usually in FileMaker (and as I get in studentpaper's layout). However, I get only an empty string.
What am I doing wrong? Does the relation in FileMaker's PHP-API differs from the "usual" FileMaker approach?
In case anybody is interested, I found the solution on my own.
The problem was that the field wasn't defined (in the layout) as normal text input, but as selection field resticted by a value list.
Thus, one needs to access the value not via the usual getField function but by one of the functions related to value lists. In my case, getValueListTwoFields did the job.

PHP PDO get all row names as objects containing value

I have a mySQL database table setup called site.
Rather than qet the column headings to produce a PHP object of values I want to produce data from the rows.
TABLE: site
:::field:::::value:::::
url www.example.com
name example site
etc Example Etcetera
So I want to be able to get this information from the server by calling the column name I want and the row I am after. I want to do this for all fields in site as I don't want to do multiple calls for the various different rows in site; I'd rather store all the information from the beginning in the object:
eg. <? echo $site['url']; ?>
I created this put it appears to be causing an error:.
$sql = "SELECT * FROM `site`";
$site[];
foreach($sodh->query($sql) as $sitefield){
$site[$sitefield['field']] = $sitefield['value'];
}
Obviously I've missed something. ¿Any idea as to what?
$site[];
should be
$site = array();

How to store search conditions with php and Codeigniter?

I have small problem.
I've coded a full website in php using CodeIgniter framework. One of my modules is search module, it contains text input with keyword and three select lists with filtering criterias.
That's ok, when I'm searching something - result's listing pagination is done via URL like that:
mysite.com/$keyword/$criteria1/$criteria2/$criteria3/$offset
works like a charm.
But when I'm entering into one of my images (it's image gallery) I want to have an option to go into NEXT and PREVIOUS image from my search results - the ones which I entered this image from.
I'm solving this case now in this way - I have session table called 'search_conditions' and I'm storing values of keyword and my three criterias there, but that's quite not comfortable, because why if someone opens second window and search something else there?
Then all of his searches in another windows or tabs are getting the same criteria - because with every new search, user overwrite the session value.
My next and previous functions:
public function next($count)
{
$search = $this->session->userdata('search_conditions'); //getting session table and overwriting it
$catid = isset($search['catid'])?$search['catid']:'0';
$brandid = isset($search['brandid'])?$search['brandid']:'0';
$prodid = isset($search['prodid'])?$search['prodid']:'0';
$keyword = isset($search['keyword'])?$search['keyword']:'';
$res = $this->search_model->main_search($keyword, $catid, $brandid, $prodid, $count, 1);
}
public function previous($count)
{
$search = $this->session->userdata('search_conditions');
$catid = isset($search['catid'])?$search['catid']:'0';
$brandid = isset($search['brandid'])?$search['brandid']:'0';
$prodid = isset($search['prodid'])?$search['prodid']:'0';
$keyword = isset($search['keyword'])?$search['keyword']:'';
$res = $this->search_model->main_search($keyword, $catid, $brandid, $prodid, $count-2, 1);
}
Can you recommend me some other, more comfortable solution, because this seems not to be good...
: )
Thank you!
Add an index to the $search_conditions variable:
$search_conditions[1]['catid']
$search_conditions[1]['brandid']
...
then refer to it with a controller's or config variable. This way you can allow one session to store multiple search conditions.
But I would recommend you drop storing the search condition in session. Instead, just pass it with the URI. Session data, in the case you describe, work as an intermediary; you don't need it. Use the Pagination Class and pass the search page number, not the direction (next or previous) to the URI.
Do not worry that the URI may look ugly - it only depends on what user searches for, and it's still friendly to share. Your only concern is if the GET string does not extend the limited length.
Pull the segments from the URI in your next() and previous() functions. Use the codeigniter URL helper. That should allow you to pass the different search criterion as variables to the next page, this would also remove your need to use the session.

Help in displaying data on user page. I think ive gone on a tangent here

I'm trying to display the data here in order of:
Author Name
Book Name
Url
NOTE: There are many results for each piece of data. Im not sure how they are stored in the array when they are fetched.
The database schema is relational as you will see and connects these bits of information from different areas of the database.
Im new to programming as you may have figured.
Im at a loss here.
Here is my code:
<?php
//Starting session
session_start();
//Includes mass includes containing all the files needed to execute the full script
//Also shows homepage elements without customs
include ('includes/mass.php');
//Set the session variable
$username = $_SESSION['username'];
//Check to see if logged in
if (isset($username))
{
//Check all databases assoc with the notes for username submissions
$sql_for_username_submission = "SELECT notes.authname, notes.bookname, notes.url, notes.note_id, notes.user, smallnotes.chapter_name FROM notes INNER JOIN small_notes ON notes.note_id = small_notes.notes_id AND notes.user = small_notes.user ORDER BY notes.note_id";
$get_data = mysql_query($sql_for_username_submission);
while ($data_row = mysql_fetch_assoc($get_data))
{
$authnames = $data_row['authname'];
Stopped here. not sure how to progress
}
}
?>
I would imagine you need some UI controls to which you would bind the data in data_row. In other words, you need to have some placeholders on the screen.
Best Regards
You are fetching my assosciative array so it does not matter. You can just reference the array item by key and use it whenever and where ever you want.
You do not have to worry about how the array is sorted.

Categories