If else statement in codeigniter - php

i want to create if-else statement in codeigniter with my logic
If $language === "english"
it will no change
$q["question_title"] = $q["question_title"]
else
it will change
$q["question_title"] = $q["question_title_**id**"]
the point is, i want to add "_id" in every php if $languege = no "english"
how to do that if php code inside of "[]" like $q["question_title"]
OR
from query.php
// translation language
$site_lang = $this->session->userdata('site_lang');
if ( $site_lang === 'english') {
$question['questions'][$i]['question_title'] = $q->quiz_text;
} else {
$question['questions'][$i]['question_title'] = $q->quiz_text_id;
}
so if i use code in /views and it read cookies in "english" it will get database "quiz_text" but if else it will get "quiz_text_id" from database
but it not work in my query code.

First echo $site_lang what you getting then keep condition simple if title has already you don't need to set same value again so do a check like
$site_lang = $this->session->userdata('site_lang');
if ( $site_lang != 'english') {
$question['questions'][$i]['question_title'] = $q->quiz_text_id;
}

Related

How to define condition based on get url value

When a user clicks on "List View" link then I want to show them the "List View" HTML and when they click on "Grid View" I want to show "Grid View" HTML.
I've defined the following links to click-
List View <br>
Grid View
Then I defined the following condition with PHP get method to show user the desired output-
<?php
if( isset( $_GET['view'] ) == 'list' ){
echo "This is List view";
}else if( isset($_GET['view'] ) == 'grid' ){
echo "This is Grid view";
}
This condition is not working. If I change "view" to "view_1" and "view_2" from URL then my condition is working as well.
<?php
if( isset( $_GET['view_1'] ) == 'list' ){
echo "This is List view";
}else if( isset($_GET['view_2'] ) == 'grid' ){
echo "This is Grid view";
}
?>
<br>
List View <br>
Grid View
But I don't want to change the "view" key. I just want to keep the same key and different value for both URL to do the conditional statement.
Is it possible?
isset() only checks if a variable exists and isn't null and returns a boolean (true/false).
To check the value, you first need to check if it exists (isset) and then check the value, like this::
if (isset($_GET['view']) && $_GET['view'] == 'list') {
// your code here.
}
You can read more here: http://php.net/manual/en/function.isset.php
Alternative
If you want to make your code and conditions slightly more readable, you could to this:
// Get the value of $_GET['view'] once, if it exists (pre PHP 7)
$view = isset($_GET['view']) ? $_GET['view'] : null;
// PHP 7.x (new shorter syntax for the above)
$view = $_GET['view'] ?? null;
if ($view == 'list') {
// your code
}
Did you try like this..
if(isset($_GET['view']))
{
if($_GET['view'] == 'list')
{
//code here
}
elseif ($_GET['view'] == 'grid')
{
//code here
}
}

How to avoid PHP "Illegal string offset warning" when comparing an variable that will either be an array or null

I am a beginner learning PHP alongside a course on Lynda.com. In the website being built, the navigation menu is made by querying a database for "subjects" and "pages" and then making a table.
Each of these "subjects" and "pages" are links that send an ID number to $_GET to refresh the page and load the specific content. The problem is either the subject array or the page array will always be null (because only one type of thing can be clicked on at once). This is causing the webpage to always throw an "Illegal string offset warning".
if (isset($_GET['subj'])) {
$sel_subject = get_subject_by_id($_GET['subj']);
$sel_page = "NULL";
} elseif (isset($_GET['page'])) {
$sel_page = get_page_by_id($_GET['page']);
$sel_subject = "NULL";
} else {
$sel_subject = "NULL";
$sel_page = "NULL";
This is at the top of the .php, and shows at least one of these variables will be NULL instead of an array.
$subject_set = get_all_subjects();
while ($subject = mysqli_fetch_array($subject_set)) {
echo "<li";
if ($subject['id'] == $sel_subject['id']) {
echo " class =\"selected\"";
}
echo "><a href=\"content.php?subj=" . urlencode($subject['id']) .
"\">
{$subject["menu_name"]}</a></li>";
$page_set = get_pages_for_subject($subject['id']);
echo "<ul class=\"pages\">";
while ($page = mysqli_fetch_array($page_set)) {
echo "<li";
if ($page['id'] == $sel_page['id']) {
echo " class =\"selected\"";
}
echo "> {$page["menu"]} </li>";
}
echo "</ul>";
}
This is the part of the code that generates the navigation menu. One of the if statements will always try to get an array value from a variable that is null.
Am I doing something wrong? Is this just an inherently bad way of making a navigation menu for the site? I know the course I am using is old, so perhaps PHP programming has made this method obsolete.
I am a huge fan of empty(). I don't care of isset can tell you that it is set as an empty string or null. I want to know if there is data, whether it's a string or an array.
Try replacing your isset calls with ! empty()
if (! empty($_GET['subj'])) {
//
} elseif (! empty($_GET['page'])) {
//
} else {
//
}
Also, be sure that you are not taking input directly from the user. If you know those _GET vars are to be integers, clean them before using them.
$page_id = (integer) $_GET['page'];
if (! empty($page_id) {
// check it out, they can't send us junk anymore!
If they aren't integers, you will have to have a more discerning method of verifying them before using them. But that is a different question, eh?

PHP While statement is showing same results as the queryresult

I'm having a little bit of trouble with my PHP function. In the database I get 2 results back from my query. But my function is doing something else. It changes when I edit '=' to '=='
MySQL Query:
SELECT ContentPages.ContentPagesID, ContentType.ContentTypeName
FROM `ContentPages` INNER JOIN ContentType ON ContentPages.ContentTypeID = ContentType.ContentTypeID INNER JOIN ContentInformation ON ContentPages.ContentInformationID = ContentInformation.ContentInformationID
Result:
ContentPagesID ContentTypeName
01425d4a-2abd-11e4-b991-525400 products01
014269dd-2abd-11e4-b991-525400 information01
PHP Function:
function GetAllPages() {
$GetAllPagesSql = "SELECT ContentPages.ContentPagesID, ContentType.ContentTypeName FROM `ContentPages` INNER JOIN ContentType ON ContentPages.ContentTypeID = ContentType.ContentTypeID INNER JOIN ContentInformation ON ContentPages.ContentInformationID = ContentInformation.ContentInformationID";
$GetAllPagesQuery = mysql_query($GetAllPagesSql);
while (($GetAllPagesRow = \mysql_fetch_array($GetAllPagesQuery)) != false) {
if($GetAllPagesRow[ContentTypeName] === 'product01') {
DisplayProducts01DesignFunction();
}
else if ($GetAllPagesRow[ContentTypeName] === 'information01') {
DisplayInformation01DesignFunction();
}
}
}
When I change:
if($GetAllPagesRow[ContentTypeName] = 'product01') {
DisplayProducts01DesignFunction();
}
else if ($GetAllPagesRow[ContentTypeName] = 'information01') {
DisplayInformation01DesignFunction();
}
To this:
if($GetAllPagesRow[ContentTypeName] === 'product01') {
DisplayProducts01DesignFunction();
}
else if ($GetAllPagesRow[ContentTypeName] === 'information01') {
DisplayInformation01DesignFunction();
}
It goes from showing function DisplayProducts01DesignFunction() twice to DisplayInformation01DesignFunction() once.
Any ideas on how to fix this?
It should actually be running it only once if your code is fixed. When you change that == to = that then doesn't remain a comparison and becomes an assignment which results to TRUE and hence your if block gets executed, which is wrong.
if($GetAllPagesRow[ContentTypeName] = 'product01')
Is not the correct way to write an if condition, if you are making your code run fine using that logic then you're doing it wrong. That = should be ==
When you say
if($GetAllPagesRow[ContentTypeName] = 'product01')
Then that if block will always be executed even if you have 100 rows.
Now you might ask ok when I put == then why doesn't the first if block work anymore? Its because you are comparing a wrong string, your database contains products01 and your if contains product01, see the missing s. Your actual if condition should be
if($GetAllPagesRow[ContentTypeName] == 'products01') {
DisplayProducts01DesignFunction();
}
else if ($GetAllPagesRow[ContentTypeName] == 'information01') {
DisplayInformation01DesignFunction();
}

Update PHP for AiContactSafe to add multiple redirects dependent on Combobox Value

This may help a few people using Aicontactsafe with joomla as the core component does not have it currently as an option. Essentially i need to change the module php file to allow different redirect urls depending on what dropdown option is selected in the "Studio" Combobox field.
Perfectly honest with you i do not know if i am typing in the correct fields or code values:
The test site you can see here - http://www.datumcreative.com/wellness/ and the form is 'book my taster class' on the right.
Below is where i believe code needs adding in the original file:
if (!array_key_exists('return_to', $parameters)) {
// initialize the database
$db = &JFactory::getDBO();
$query = 'SELECT redirect_on_success FROM #__aicontactsafe_profiles WHERE id = ' . $pf;
$db->setQuery( $query );
$redirect_on_success = $db->loadResult();
// set the return link to the current url or the one setup into the profile
if (strlen(trim($redirect_on_success)) == 0) {
$postData['return_to'] = $currentUrl;
} else {
$postData['return_to'] = $redirect_on_success;
}
}
To create the other redirects i added the following custom lines, below the line "$redirect_on_success = $db->loadResult();":
$redirect_to_wgc = "http://www.google.com";
$redirect_to_harp = "http://www.yahoo.co.uk";
I then needed to add the "if" statements **.
**if ($field_values['aics_studio'] == "Welwyn Garden City") {
$postData['return_to'] = $redirect_to_wgc;
}
elseif ($field_values['$aics_studio'] == "Harpenden") {
$postData['return_to'] = $redirect_to_harp;
}**
elseif (strlen(trim($redirect_on_success)) == 0) {
$postData['return_to'] = $currentUrl;
} else {
$postData['return_to'] = $redirect_on_success;
}
No luck so far, any suggestions?

Search not working as intended

Why won't my search function ever execute the "else" (else should echo a text when no resulsts haved been found)? I also have some problems when trying to show all results (with no search criterias selected, just by pressing the search button). I'll upload the whole code of the page because I don't know if you need the HTML part as well or not to figure out the problem. I know it's a big chunk of code but please help out if you can. Thanks!
Here's a link to my code: http://pastebin.com/BXe1C0dr
This is not yet the answer, just a brief code structure of Matei Panchios code. Because it is hard to make sense of long code, so I try to simplify it so that other people might be able to help.
$termeni = mysql_real_escape_string($_POST['termeni']);
$tip=$_POST['tip'];
$judet=$_POST['judet'];
if((!empty($termeni)) and (isset($tip)) and (isset($judet))) {
$query = "SELECT * FROM oferte WHERE localitate LIKE '%$termeni%' AND
tip_locatie='$tip' AND judet='$judet'";
// do the query and write some HTML
} elseif (isset($tip)) {
$query = "SELECT * FROM oferte WHERE tip_locatie='$tip'";
// do the query and write some HTML
} elseif (isset($judet)) {
$query = "SELECT * FROM oferte WHERE judet='$judet'";
// do the query and write some HTML
} elseif (!empty($termeni)) {
...
} elseif (!empty($termeni) AND (isset($judet))) {
...
} elseif (!empty($termeni) AND (isset($tip))) {
...
} elseif ((isset($judet)) AND (isset($tip))) {
...
} elseif ((!isset($judet)) AND (!isset($tip)) AND (empty($termeni))) {
...
} else {
// I believe this where it does not get executed.
}
Well, it makes sense why it does not get executed because there is other way that the elseif does not cover. If you look from this point of view
If three variable is set
if((!empty($termeni)) and (isset($tip)) and (isset($judet))) {
If two variables is set
elseif (!empty($termeni) AND (isset($judet)))
elseif (!empty($termeni) AND (isset($tip)))
elseif (!empty($termeni) AND (isset($tip)))
elseif (!empty($termeni) AND (isset($tip)))
If one variable is set
elseif (isset($tip))
elseif (isset($judet))
elseif (!empty($termeni))
When no variable is set
elseif ((!isset($judet)) AND (!isset($tip)) AND (empty($termeni)))
which leave else condition with nothing to cover.
If I were you, I would structure the code as following.
if (!empty($termeni) and isset($tip) and isset($judet)) {
query = '....';
} elseif (!empty($termeni) and isset($judet) {
query = '....';
} // .... the rest of the condition
$result = mysql_query($query);
if (mysql_num_rows($result) > 0) {
// write HTML table
} else {
// write message that there is no result found
}
This will reduce the size of your code by 6 times.

Categories