I need a little help on getting a rule in to output something else if a certain variable is called.
To break it down I have the following listed:
private $zebra_moto_symbol = array
( "ES400", "MC9500", "MC9200", "MC9190", "MC9094", "MC9090", "MC9097", "MC9060",;
and using this code it pulls the models into the page in a list:
public function manufacturer_models_list() {
$manu_name = $this->manufacturer_name;
$output = "<ul>";
sort($this->$manu_name);
foreach($this->$manu_name as $model) {
$output .= "<li>" . "" . $model . "</li>";
}
$output .= "</ul>";
$output .= "<p class=\"clear\"></p>";
$output .= "Arrange A Repair";
return $output;
}
On all but two of these, I need it display the repair.php link, however on two these need to be different. What would I need to input to make this happen?
Thanks in advance (sorry, this one stumped me).
:)
You can use a switch statement for this.
<?
public function manufacturer_models_list() {
$manu_name = $this->manufacturer_name;
$output = "<ul>";
sort($this->$manu_name);
foreach ($this->$manu_name as $model) {
switch($model) {
//Output NOT repair.php on this list of strings
case "ES400":
case "MC9500":
$output .= "<li>DIFFERENT OUTPUT</a></li>";
break;
//default is the action that happens if none of the previous conditions are met
default:
$output .= "<li>" . "" . $model . "</li>";
break;
}
}
$output .= "</ul>";
$output .= "<p class=\"clear\"></p>";
$output .= "Arrange A Repair";
return $output;
}
?>
Read more about Switch Statements
If I understood correctly, what you want is to have a different output on certain values.
I would think about have another array to hold the values that you want a different output and you can do something like this:
$different_output_array = ['ES400', 'MC9500']; # you can add new elements any time
and just modify your function to something like this:
public function manufacturer_models_list() {
$manu_name = $this->manufacturer_name;
$output = "<ul>";
sort($this->$manu_name);
foreach($this->$manu_name as $model) {
if(in_array($model,$different_output_array))
{
$output .= "<li>" . "" . $model . "</li>";
}
else
{
$output .= "<li>" . "" . $model . "</li>";
}
}
$output .= "</ul>";
$output .= "<p class=\"clear\"></p>";
$output .= "Arrange A Repair";
return $output;
}
Hope this can help.
Related
I'm trying to show a object in a html modal. Since I don't know the structure beforehand, and the child properties can also be objects or arrays, a simple recursive function seemed to be the way to go, but Im not sure. I have this:
<?php
class LogHelper extends Helper
{
public function warningLogError($body_error)
{
$data = [];
$html = '';
$html .= "<div name='bodyErrosr' class = 'form-group'>";
$html .= "<div class = 'alert alert-warning' role = 'alert'>";
if (!empty($body_error)) {
foreach($body_error as $key => $value) {
$html .= "<li><b>" . $key . "= " . $value. "</b></li>";
}
} else {
$html .= "<li><b>" . 'Error!' . "</b></li>";
}
$html .= "</ul>";
$html .= "</div>";
$html .= "</div>";
return $html;
}
}
But I don't know the structure beforehand, sometimes I receive an object inside object multiple times. How is the best way to do this?
Have a look at: https://packagist.org/packages/symfony/var-dumper
This also handles circular references (where print_r/vardump fail)
I am trying to echo the data from the following table titled sectionamain, however I am only getting the data from the first row (TitleID = 1)
|TitleID | MainTitle |
|1 |This is a title |
|2 |This is another title |
The below is the PHP code i am using:
function get_practices_titles() {
global $connection;
$query = "SELECT * ";
$query .= "FROM sectionamain ";
$query .= "ORDER BY TitleID ASC";
$A_list = mysqli_query($connection, $query);
confirm_query($A_list);
return $A_list;
}
function practices_titles() {
$output = "<div class=\"container inner\">\n";
$output .= "<div class=\"wrap\">\n";
$A_set = get_practices_titles();
while ($list_A = mysqli_fetch_assoc($A_set)) {
$output .= "<h1 class\=showcontent\" id=";
$output .= htmlentities($list_A["TitleID"]);
$output .= ">";
$output .= htmlentities($list_A["MainTitle"]);
$output .= "</h1>";
mysqli_free_result($A_set);
$output .="</div>\n</div>";
return $output;
};
}
I thank you all in advance for your help.
I believe you need to move mysqli_free_result($A_set); to outside of your while loop.
You are getting rid of your results before you are done looping through them. Move it to after your while loop is finished.
From the docs: http://php.net/manual/en/mysqli-result.free.php
You should always free your result with mysqli_free_result(), when
your result object is not needed anymore.
But you still need it - you are freeing the memory after looping through only one result.
Remove the following 3 lines from the loop to the end of the function :
mysqli_free_result($A_set);
$output .="</div>\n</div>";
return $output;
so your function will be something like this:
function practices_titles() {
$output = "<div class=\"container inner\">\n";
$output .= "<div class=\"wrap\">\n";
$A_set = get_practices_titles();
while ($list_A = mysqli_fetch_assoc($A_set)) {
$output .= "<h1 class\=showcontent\" id=";
$output .= htmlentities($list_A["TitleID"]);
$output .= ">";
$output .= htmlentities($list_A["MainTitle"]);
$output .= "</h1>";
};
mysqli_free_result($A_set);
$output .="</div>\n</div>";
return $output;
}
I'm pretty new to php and I can't figure out why 'testimonial_text' is not being wrapped within the 'testimonial-text' class. For some reason, it's outputting 3 elements and one of them is "testimonial text" but it's not within the "testimonial-text". "testimonial_author" is being correctly wrapped in "testimonial-author". Any ideas?
<?php
$rows = get_field('testimonials');
if($rows) {
foreach($rows as $row) {
$output = "<div class = 'testimonial-container'>";
$output .= "<p class = 'testimonial-text'>".$row['testimonial_text'] . "</p>";
$output .= "<p class = 'testimonial-author'>".$row['testimonial_author'] . "</p>";
$output .= "</div>";
echo $output;
}
}
?>
Following the image showing the contents of $rows in the comments it looks like the data you are returning has extra code and/or quotes in it. So I would recommend doing something like..
if($rows) {
$output = '';
foreach($rows as $row) {
$output .= "<div class = 'testimonial-container'>";
$output .= "<p class = 'testimonial-text'>" . strip_tags ($row['testimonial_text']) . "</p>";
$output .= "<p class = 'testimonial-author'>" . strip_tags ($row['testimonial_author']) . "</p>";
$output .= "</div>";
}
echo $output;
}
To remove any stray code that is getting output.
Worth noting #Naumov said to use strip_tags as well :)
I am working on a CodeIgniter application.
A part of the nav menu for my application is generated from using session data. Since, I have to print the same thing at a number of places, I wrote a function to do the printing. The file which creates the menu is given below. The function print_roles_assigned() is used a number of times in this file.
$roles_assigned = $this->session->userdata('roles_assigned');
function print_roles_assigned() {
$output = '';
if ($roles_assigned)
{
foreach ($roles_assigned as $role) {
$output .= '<li>' . anchor('main/home/'.$role->role_name, $role->rol
e_name) . '</li>';
}
}
else
{
$output .= '<li>No roles have been assigned.</li>';
}
return $output;
}
The code given above didn't work. Out of any options, I resorted to using a $GLOBAL. An issue like this has never happened with me before and I am not sure if the use of $GLOBAL is appropriate. The new code is given below:
$GLOBALS['roles_assigned'] = $this->session->userdata('roles_assigned'); // Change made here
function print_roles_assigned() {
$output = '';
$roles_assigned = $GLOBALS['roles_assigned']; // Using the global variable inside function
if ($roles_assigned)
{
foreach ($roles_assigned as $role) {
$output .= '<li>' . anchor('main/home/'.$role->role_name, $role->rol
e_name) . '</li>';
}
}
else
{
$output .= '<li>No roles have been assigned.</li>';
}
return $output;
}
I would like to know:
Why my initial code failed to work?
Is the use of $GLOBAL appropriate?
What can be an alternate method to fix this issue?
The initial code failed because your $roles_assigned in not injected. Parameterise the function to "function print_roles_assigned($roles_assigned) { .. } " to access the $roles_assigned variable.
Why my initial code failed to work?
Each function has what is called variable scope. Variables declared outside of the function are not accessible from within the function unless they are passed in as parameters, are members of a class that the function is a member of, or are explicitly declared to be global.
There are three different ways to do this, take your pick.
The easiest way to do this is to just pass the function in as a parameter I.E.
$roles_assigned = $this->session->userdata('roles_assigned');
function print_roles_assigned($roles_assigned) {
$output = '';
if ($roles_assigned)
{
foreach ($roles_assigned as $role) {
$output .= '<li>' . anchor('main/home/'.$role->role_name, $role->rol
e_name) . '</li>';
}
}
else
{
$output .= '<li>No roles have been assigned.</li>';
}
return $output;
}
Another option is to make $roles_assigned a member of the class, I.E.
$this->roles_assigned = $this->session->userdata('roles_assigned');
function print_roles_assigned() {
$output = '';
if ($this->roles_assigned)
{
foreach ($this->roles_assigned as $role) {
$output .= '<li>' . anchor('main/home/'.$role->role_name, $role->rol
e_name) . '</li>';
}
}
else
{
$output .= '<li>No roles have been assigned.</li>';
}
return $output;
}
The other option (not recommended) is to use the global keyword I.E.
$roles_assigned = $this->session->userdata('roles_assigned');
function print_roles_assigned() {
global $roles_assigned;
$output = '';
if ($roles_assigned)
{
foreach ($roles_assigned as $role) {
$output .= '<li>' . anchor('main/home/'.$role->role_name, $role->rol
e_name) . '</li>';
}
}
else
{
$output .= '<li>No roles have been assigned.</li>';
}
return $output;
}
This sounds like a scope issue. Try using $this->roles_assigned for your reference to that array.
IMO, it's not good-practise to use GLOBAL in this way.
If so.... any idea how?
You might be interested in this post about adding sessions to the profiler Basically it works by creating a MY_Profiler.php file and copy and pasting this code:
<?php if(!defined('BASEPATH')) exit('No direct script access allowed');
class MY_Profiler extends CI_Profiler {
/**
* Adds session data to the profiler
* Adds a table row for each item of session data with the key and value
* Shows both CI session data and custom session data
*/
function _compile_session() {
$output = "\n\n";
$output .= '<fieldset style="border:1px solid #009999;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
$output .= "\n";
$output .= '<legend style="color:#009999;"> '.'SESSION DATA'.' </legend>';
$output .= "\n";
if (!is_object($this->CI->session)) {
$output .= "<div style='color:#009999;font-weight:normal;padding:4px 0 4px 0'>".'No SESSION data exists'."</div>";
} else {
$output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";
$sess = get_object_vars($this->CI->session);
foreach ($sess['userdata'] as $key => $val) {
if ( ! is_numeric($key)) {
$key = "'".$key."'";
}
$output .= "<tr><td width='50%' style='color:#000;background-color:#ddd;'>$_SESSION[".$key."] </td><td width='50%' style='color:#009999;font-weight:normal;background-color:#ddd;'>";
if (is_array($val)) {
$output .= "<pre>" . htmlspecialchars(stripslashes(print_r($val, true))) . "</pre>";
} else {
$output .= htmlspecialchars(stripslashes($val));
}
$output .= "</td></tr>\n";
}
$output .= "</table>\n";
}
$output .= "</fieldset>";
return $output;
}
function run() {
$output = "<div id='codeigniter_profiler' style='clear:both;background-color:#fff;padding:10px;'>";
$output .= $this->_compile_uri_string();
$output .= $this->_compile_controller_info();
$output .= $this->_compile_memory_usage();
$output .= $this->_compile_benchmarks();
$output .= $this->_compile_get();
$output .= $this->_compile_post();
$output .= $this->_compile_queries();
$output .= $this->_compile_session();
$output .= '</div>';
return $output;
}
}
Of course you can, just create a MY_profiler and add two methods: run() and _compile_session()
run() is the same as the parent just copy the code & add the _compile_session at the end and _compile_session could have the same code as _compile_post, just change $_POST to $_SESSION