PHP include top file and bottom file containing ifelse curly braces - include

How can I include_once a php file at the top that contains an if statement with an open curly brace,
And then include a file at the bottom that contains the closing curly brace to that if statement with more if statements preceding it?
<?php include_once('includes/authtop.php');?>
<div id="mydiv">
// stuff here
</div>
<?php include_once('includes/authbottom.php');?>
**Note**: authbottom.php contains several ifelse statements and displays data accordingly.
I need to do this to have more clean and organized code but when I move the code into the includes I get an error, it's as if the rendered page does not recognize the curly braces for open close?
If its to do with a function or function/class could someone please give me an example.
This is the top file: authtop.php
if( $loggedin ) {
if( $accessLevel == 0 ) {
This is the bottom file: authbottom.php
} elseif() {
// do this
} elseif() {
// do this
} elseif() {
// do this
} else {
// do this
}

You should not be including files to handle conditions in that manner. It's messy and error prone; as you have discovered.
Instead, refactor your code.
Here is an example of that...
function someFunc() {
if( $loggedin ) {
if( $accessLevel == 0 ) {
return TRUE;
} elseif() {
// do this
} elseif() {
// do this
} elseif() {
// do this
} else {
// do this
}
}
if (someFunc()) {
// stuff here
}

I'd use a switch
include('authtop.php);
switch($accesslevel) {
default:
case 0: include('filepath.php'); break;
case 1: include('filepath.php'); break;
case 2: include('filepath.php'); break;
case 3: include('filepath.php'); break;
case 4: include('filepath.php'); break;
}
include('authbottom.php');
or....
switch($accesslevel) {
default:
case 0:
if($paid==0) { $pd='THANKS'; } else { $pd='DONATE!'; }
$lvl = 'Guest';
/* whatever you need to include for level 0 here */
break;
case 1:
if($paid==0) { $pd='THANKS'; } else { $pd='DONATE!'; }
$lvl = 'User';
/* whatever you need to include for level 1 here */
break;
case 2:
if($paid==0) { $pd='THANKS'; } else { $pd='DONATE!'; }
$lvl = 'Moderator';
/* whatever you need to include for level 2 here */
break;
case 3:
if($paid==0) { $pd='THANKS'; } else { $pd='DONATE!'; }
$lvl = 'Super Moderator';
/* whatever you need to include for level 3 here */
break;
case 4:
if($paid==0) { $pd='THANKS'; } else { $pd='DONATE!'; }
$lvl = 'Admin';
/* whatever you need to include for level 4 here */
break;
}
echo '
<div class="myDiv">'.$pd.'</div>
<div class="myDiv2">'.$lvl.'</div> ';
The HTML will show whatever the $pd variable has been set at according to the switch.
And it's best to set up the include so they are self contained without any open braces or brackets.

Related

[eyeshot]How can I add a new Viewport into a viewer

what is the best way to add a new viewport into the viewer with the same drawing?
Regards Jürgen
You can check the Custom ViewportLayout source code sample.
Here is the code extracted from the above sample
private static void InitializeViewportsByLayoutType(Design design, viewportLayoutType layout)
{
int viewportsNumber;
switch (layout)
{
case viewportLayoutType.SingleViewport:
viewportsNumber = 1;
break;
case viewportLayoutType.TwoViewportsVertical:
case viewportLayoutType.TwoViewportsHorizontal:
viewportsNumber = 2;
break;
case viewportLayoutType.ThreeViewportsWithOneOnBottom:
case viewportLayoutType.ThreeViewportsWithOneOnLeft:
case viewportLayoutType.ThreeViewportsWithOneOnRight:
case viewportLayoutType.ThreeViewportsWithOneOnTop:
viewportsNumber = 3;
break;
case viewportLayoutType.FourViewports:
case viewportLayoutType.Stacked:
viewportsNumber = 4;
break;
default:
viewportsNumber = 1;
break;
}
if (design.Viewports.Count > viewportsNumber)
{
while (design.Viewports.Count > viewportsNumber)
design.Viewports.RemoveAt(design.Viewports.Count - 1);
}
else
{
while (design.Viewports.Count < viewportsNumber)
{
design.Viewports.Add((Viewport)design.Viewports[0].Clone());
}
}
// When changing the LayoutMode, the UpdateViewportsSizeAndLocation() method is called as well.
design.LayoutMode = layout;
}

How to display and hide columns based on the two dropdown selection using ag grid

Basically, I have two dropdowns a and b.
Based on the combination of these two dropdowns Ineed to hide / show colums using ag grid.
Eg: if I choose 'xyz' in dropdown a and '123' in dropdown b, 2 columns wiil be displayed. Similarly if choose dropdown 'ghj' and '456' in dropdown b, some ther 3 columns will be selected and the first 2 columns will be no longer be visible / available.
I can use if else conditionsbut I need to check for all the possible combinations. Is there an easy way to do so?
dropdown a
onReportingType(e) {
// console.log(e);
this.reportData = e;
this.reportSelArr.push(this.reportData);
console.log(this.reportSelArr);
}
dropdown b
onDataPoint(e) {
console.log(e);
this.dataPointData = e;
this.dataPointSelArr.push(this.dataPointData);
console.log(this.dataPointSelArr);
this.addRatingCol();
}
Condition applied for now
addRatingCol() {
// console.log(this.reportSelArr);
// console.log(this.dataPointSelArr);
for (let i = 0; i < this.reportSelArr.length; i++) {
for (let j = 0; j < this.dataPointSelArr.length; j++) {
if (this.reportSelArr[i].reportingTypeName === 'Outstanding') {
if (this.dataPointSelArr[j].dataPointName === 'Rating') {
this.gridColumnApi.setColumnsVisible(['newRatingName', 'newRatingReleaseDate'], true);
return true;
} else if (this.dataPointSelArr[j].dataPointName === 'Rating Outlook') {
this.gridColumnApi.setColumnsVisible(['newOutlookName', 'newOutlookDate', 'outlookEndDate'], true);
} else if (this.dataPointSelArr[j].dataPointName === 'Rating Watch') {
this.gridColumnApi.setColumnsVisible(['newRatingWatchName', 'newRatingWatchDate', 'ratingwatchEndDate'], true);
}
}
} // end of the for loop
}
if (this.addCol === true && this.addReport === true){
this.gridColumnApi.setColumnsVisible(['newRatingName', 'newRatingReleaseDate'], true);
} else {
this.gridColumnApi.setColumnsVisible(['newRatingName', 'newRatingReleaseDate'], false);
}
}
If you are using ES6..
Instead of the 2 for loops i would prefer using filter for the first loop and finding whether it has the reporting type as outstanding.
const outstanding = this.reportSelArr.filter(elem => elem.reportingTypeName === 'Outstanding')
i would then use the return value of the filter to determine whether or not to run the second for loop which can be through a map.
if (outstanding) {
this.dataPointSelArr.map(elem => {
const { dataPointName } = elem
switch(dataPointName) {
case 'Rating': {
//statements;
break;
}
case 'Rating Outlook': {
//statements;
break;
}
case 'Rating Watch':{
//statements;
break;
}
default: {
//statements;
break;
}
}
})
}

how to check count of likes and dislike for post for rating sysytem

i am making an system to check count of likes and dislike if like count is more then dislike count then it gives true
but i am getting an error
// if (Files::withCount('likes') >= Files::withCount('dislike')) {
// return response()->json(['true']);
// }elseif (Files::withCount('dislike') >= Files::withCount('like')) {
// return response()->json(['false']);
// }else{
// return response()->json(['error'=>'somethingwenrwrng']);
// }
// if( DB::table('files')->select('files_id')
// ->join('likes','files_id.files_id','=','files_id') > DB::table('files')->select('id')
// ->join('dislike','files_id.files_id','=','files_id') ){
// return response()->json(['true']);
// }else {
// return response()->json(['error'=>'somethingwenrwrng']);
// }
$file = Files::find($id);
if($file ->likes->count() > $file ->dislike->count() ){
return response()->json(['true']);
}else{
return response()->json(['error'=>'somethingwenrwrng']);
}
i have tried different method to check but getting an error
the withCount() method return a property of the related field_count counting related models
so
$file = Files::find($id)->withCount(['likes','dislikes']);
if($file->likes_count > $file->dislikes_count ){
return response()->json(['true']);
}

Modify the MY_Router.php file for QUERY STRING Codeigniter 3.0.6

I use codeigniter 3.0.6 query string like
index.php?d=directoryt&c=controller
index.php?d=directory&c=controller&m=function
How ever having two get methods for directory and controller is a bit long.
Question Is there any way to modify the protected function
_set_routing() function using a MY_Router.php to get it so it will pick up the directory and controller by using one query only like example below.
index.php?route=directory/controller
// If need to get function
index.php?route=directory/controller&m=function
What have tried so far
<?php
class MY_Router extends CI_Router {
protected function _set_routing()
{
// Load the routes.php file. It would be great if we could
// skip this for enable_query_strings = TRUE, but then
// default_controller would be empty ...
if (file_exists(APPPATH.'config/routes.php'))
{
include(APPPATH.'config/routes.php');
}
if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/routes.php'))
{
include(APPPATH.'config/'.ENVIRONMENT.'/routes.php');
}
// Validate & get reserved routes
if (isset($route) && is_array($route))
{
isset($route['default_controller']) && $this->default_controller = $route['default_controller'];
isset($route['translate_uri_dashes']) && $this->translate_uri_dashes = $route['translate_uri_dashes'];
unset($route['default_controller'], $route['translate_uri_dashes']);
$this->routes = $route;
}
// Are query strings enabled in the config file? Normally CI doesn't utilize query strings
// since URI segments are more search-engine friendly, but they can optionally be used.
// If this feature is enabled, we will gather the directory/class/method a little differently
if ($this->enable_query_strings)
{
// If the directory is set at this time, it means an override exists, so skip the checks
if ( ! isset($this->directory))
{
$_route = isset($_GET['route']) ? trim($_GET['route'], " \t\n\r\0\x0B/") : '';
if ($_route !== '')
{
echo $_route;
$this->uri->filter_uri($_route);
$this->set_directory($_route);
}
}
// Routing rules don't apply to query strings and we don't need to detect
// directories, so we're done here
return;
}
// Is there anything to parse?
if ($this->uri->uri_string !== '')
{
$this->_parse_routes();
}
else
{
$this->_set_default_controller();
}
}
}
config.php
$config['allow_get_array'] = TRUE;
$config['enable_query_strings'] = TRUE;
$config['controller_trigger'] = 'c';
$config['function_trigger'] = 'm';
$config['directory_trigger'] = 'd';
// Modifyed in MY_Router.php
$config['route'] = 'route';
I have it working
<?php
class MY_Router extends CI_Router {
protected function _set_routing() {
if (file_exists(APPPATH.'config/routes.php'))
{
include(APPPATH.'config/routes.php');
}
if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/routes.php'))
{
include(APPPATH.'config/'.ENVIRONMENT.'/routes.php');
}
// Validate & get reserved routes
if (isset($route) && is_array($route))
{
isset($route['default_controller']) && $this->default_controller = $route['default_controller'];
isset($route['translate_uri_dashes']) && $this->translate_uri_dashes = $route['translate_uri_dashes'];
unset($route['default_controller'], $route['translate_uri_dashes']);
$this->routes = $route;
}
if ($this->enable_query_strings) {
if ( ! isset($this->directory))
{
$route = isset($_GET['route']) ? trim($_GET['route'], " \t\n\r\0\x0B/") : '';
if ($route !== '')
{
$part = explode('/', $route);
$this->uri->filter_uri($part[0]);
$this->set_directory($part[0]);
if ( ! empty($part[1])) {
$this->uri->filter_uri($part[1]);
$this->set_class($part[1]);
// Testing function atm
if ( ! empty($_GET['function']))
{
$this->uri->filter_uri($_GET['function']);
$this->set_method($_GET['function']);
}
$this->uri->rsegments = array(
1 => $this->class,
2 => $this->method
);
}
} else {
$this->_set_default_controller();
}
}
// Routing rules don't apply to query strings and we don't need to detect
// directories, so we're done here
return;
}
// Is there anything to parse?
if ($this->uri->uri_string !== '')
{
$this->_parse_routes();
}
else
{
$this->_set_default_controller();
}
}
}

smarty path problem

here is my folder
index.php
smartyhere
-Smarty.class.php
admin
-index.php
-users.php
in index.php -> $smarty->display('index.tpl');
in admin/index.php -> $smarty->display('adminindex.tpl');
got error Smarty error: unable to read resource: "adminindex.tpl"
any idea ?
thx
try to understand code
which urlencode is doing path
<?php
print_r($file);
if (isset($file)) {
$var = explode("-", $file);
print_r($var);
$prefix = $var[0];
$script = $var[1];
} else {
$file = "c-home1";
$prefix = "c";
$script = "home";
$modid = 0;
}
if ($script=="") {
$script="prod_list";
}
/*
* following code finds out the modules from suffix
* and find out the script name
*/
switch ($prefix) {
case "c":
$module = "content";
break;
case "m":
$module = "myaccount";
break;
default:
$module = "content";
break;
}
$smarty->assign("module",$module);
/*
* following code finds out the modules from suffix and
* find out the script name
*/
$include_script .= $module."/".$script.".php";
if (file_exists($include_script)) {
include_once $include_script;
} else {
include_once "content/error.php";
}
if ($script!='home') {
if ($script == 'termsandcondition') {
$smarty->display("content/termsandcondition.tpl");
} else {
$smarty->display("template.tpl");
}
} else {
$smarty->display("template_home.tpl");
$smarty->assign("msg", $msg);
$smarty->assign("msglogin", $msglogin);
}

Resources