How to get getRequestUri() in layout.phtml - html

In my zend layout.phtml I am serving my navigation like this:
<?php
$userInfo = new Zend_Session_Namespace('userInfo');
if($userInfo->userType=='admin')
{
echo '<li >home</li>';
echo '<li >Addbooks</li>';
echo '<li class="selected">EditBook</li>';
echo '<li>Adduser</li>';
echo '<li>Logout</li>';
}
?>
in a normal page I can get url this way
$this->view->assign('url',$this->getRequest()->getRequestUri());
$url = $this->getRequest()->getRequestUri();
$b=basename($url);
$this->view->assign('b',$b);
I want this url in my layout.phtm how can I pass this from bootstrap.php to layout.phtml? here my requirement is to add a class to li like this:
<li <?php if($b==EditBook) echo 'class="selected' ?> ">EditBook</li>

Better way to obtain it, is to write a view helper that will assign necessary variables
class Helper_Params extends Zend_Controller_Action_Helper_Abstract {
$view = $this->getActionController()->view;
$request = $this->getRequest();
$view->requestUri = $request->getRequestUri();
}
and init it in your Bootstrap
public function _initHelpers() {
Zend_Controller_Action_HelperBroker::addHelper ( new Helper_Params () );
}

Related

How to display Name in codeigniter

Please help me, I want to display the name of the users from the table according to sender_id and recipient_id, how do the code model and view in my CodeIgniter
Tables schema
Data Table users
Data Table Messages
Model
function get_pesan_view(){
$this->db->from('messages');
$this->db->join('users', 'users.id = messages.sender_id', 'left');
$this->db->join('users', 'users.id = messages.recipient_id', 'left');
$q2 = $this->db->get();
return $q2->result();
}
Contoller
public function view_pesan()
{
$data['get_pesan']=$this->Emp_model->get_pesan_view();
$this->load->view('employee/top');
$this->load->view('employee/nav', $data);
$this->load->view('employee/slidbar', $data);
$this->load->view('employee/pesan', $data);
$this->load->view('employee/bottom');
}
View
<?php foreach ($get_pesan as $pesan) { ?>
<strong> Sender : <?php echo $pesan->name ; ?> </strong>
<strong> Recipient : <?php echo $pesan->name ; ?> </strong>
<a> <?php echo $pesan->body ; ?></a>
<?php } ?>
I want to display the name of the sender and recipient together in view
Controller
public function get_names(){
$this->load->model('model_name'); //If not loaded in the autoload.php file
$names = $this->model_name->all_names();
$this->load->view('view_file_name', compact('names'));
}
The query in the model will be like this
public function all_names(){
$this->db->select('name');
$this->db->from('users');
$this->db->join('messages', 'users.id = messages.sender_id');
return $this->db->get()->result();
}
To join with recipient_id do like this
$this->db->join('messages', 'users.id = messages.recipient_id');
Then use foreach loop to show all names in the view
foreach($names as $row){
echo $row->name;
}
model
$this->db->select('messages.*,u1.{name of field for username} as sender_name,u2.{name of field for username} as recipient_name');
$this->db->from('messages');
$this->db->join('users u1', ' u1.id = messages.sender_id', 'left');
$this->db->join('users u2', ' u2.id = messages.recipient_id', 'left');
$q2 = $this->db->get();
return $q2->result();
View
<?php foreach ($get_pesan as $pesan) { ?>
<strong> Sender : <?php echo $pesan->sender_name; ?> </strong>
<strong> Recipient : <?php echo $pesan->recipient_name; ?> </strong>
<a> <?php echo $pesan->body ; ?></a>
<?php } ?>
i have forgotten you cannot join one tale more than one time you need to use table aliases. it will work for you/ just place the name what ever fild name is in your data base.
it will work or else share me screen short of your table and error pages

How can I group subtopics into topics in this MySQL statement?

I've tried GROUP BY with my data below but it only brings back one subtopic. How can I return all the subtopics and organise them under each topic without the topic_name appearing with each subtopic_name.
Edit: Included a screenshot of the page and here is the PHP used:
<ul class="topics-list">
<?php
foreach ($data as $key){
foreach ($key as $item){
$topic_name = $item['topic_name'];
$subtopic_name = ucwords($item['subtopic_name']);
?>
<div class="the_topic">
<h2 class="topic_change"><?php echo $topic_name; ?></h2>
<ul><li class="subtopic_name"><h3><?php echo $subtopic_name; ?></h3></li></ul>
<hr />
</div>
<?php } ?>
<?php } ?>
</ul>
You could use GROUP_CONCAT() to concatenate all subtopics into one string per topic, and then parse the string in your application code.
SELECT topic_name, GROUP_CONCAT(subtopic_name DELIMITER 'ยงยงยง') as subtopic_names
FROM questions2
GROUP BY topic_name
But i do not recommend that, because you will get in troubles, if a subtopic contains your delimiter. I would just use your second query and group the result in the application code.
PHP code would look something like:
// group the data
$groupedData = array();
foreach ($data as $item) {
$topic_name = $item['topic_name'];
$subtopic_name = ucwords($item['subtopic_name']);
$groupedData[$topic_name][] = $subtopic_name;
}
// grouped output
foreach ($groupedData as $topic_name => $subtopic_names) {
echo '<div class="the_topic">';
echo '<h2 class="topic_change">' . $topic_name . '</h2><ul>';
foreach ($subtopic_names as $subtopic_name) {
echo '<li class="subtopic_name"><a href="#" data-toggle="modal" data-target="#lvlModal"><h3>';
echo $subtopic_name;
echo '</h3></a></li>';
}
echo '</ul><hr /></div>';
}

Upload file to GDrive in php without user intervention

I am using google-drive-sdk to upload a pdf file (dynamically generated in the php web app) to Gdrive.
About my app
On click of submit button the users see a THANKYOU displayed on the page. At the background
pdf file gets generated
and this file needs to be uploaded to Gdrive account --(configured the client id & secret key for the app in Google Console)
:
I am able to upload to the configured GDrive but first time to authenticate I run the url(redirect url given in Google Console) in browser(*say
http://localhost:3422/wordpress/wp-content/plugins/mktProc/google-api-php-client/gDrive_access/pdf_results_upload.php
*)
This takes me to GDrve page and prompts Allow\Deny Access.
I click Allow and then am able to upload to Grdive.
But I would like this to happen without prompts for Allow\Deny for the user to interact.
Please give me details on what I need to add so that when user clicks the Submit button , automatically the generated file gets inserted to GDrive.
I read lot in net but couldn't understand and get it to work.
Please help me with steps and code to include.
The code I am using
<?php
include_once "templates/base.php";
if(!session_id()){session_start();}
require_once realpath(dirname(__FILE__) . '/../autoload.php');
$pdf_filename='';
try
{
if(isset($_SESSION['pdf_filename']) && $_SESSION['pdf_filename'])
$pdf_filename=$_SESSION['pdf_filename'];
}
catch(Exception $ex)
{
echo ("This is for GDrive Upload from CalWeb");
}
$pfdStoragePath=dirname(__FILE__)."/../../vendor/pdf/".$pdf_filename;
/************************************************
ATTENTION: Fill in these values! Make sure
the redirect URI is to this page, e.g:
http://localhost:8080/fileupload.php
************************************************/
$client_id = 'xxxx.apps.googleusercontent.com';
$client_secret = 'xxxxSB0tNVy';
$redirect_uri = 'http://localhost:3422/wordpress/wp-content/plugins/mktProc/google-api-php-client/gDrive_access/roi_results_upload.php';
$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->addScope("https://www.googleapis.com/auth/drive");
$client->setAccessType("offline");
$client->setApprovalPrompt('force');
$service = new Google_Service_Drive($client);
if (isset($_REQUEST['logout'])) {
unset($_SESSION['upload_token']);
}
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['upload_token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
if (isset($_SESSION['upload_token']) && $_SESSION['upload_token']) {
$client->setAccessToken($_SESSION['upload_token']);
if ($client->isAccessTokenExpired()) {
unset($_SESSION['upload_token']);
}
} else {
$authUrl = $client->createAuthUrl();
}
/************************************************
If we're signed in then lets try to upload our
file.
************************************************/
try {
if ($client->getAccessToken()) {
if($pdf_filename!=="")
{
$file = new Google_Service_Drive_DriveFile();
$file->setTitle($pdf_filename);
$file->setDescription('Document');
$file->setMimeType('application/pdf');
$result = $service->files->insert(
$file,
array(
'data' => file_get_contents($pfdStoragePath),
'mimeType' => 'application/pdf',
'uploadType' => 'multipart'
)
);
//echo $result->webContentLink;
$pdf_filename="";
unset($_SESSION['pdf_filename']);
}
//if (isset($result) && $result) $_SESSION['webContentLink']=$result->webContentLink;
//echo $result->webContentLink;
}//if ($client->getAccessToken()) {
} catch (Exception $ex) {
echo '';
}
?>
<div class="box">
<div class="request">
<?php if (isset($authUrl)): ?>
<a class='login' href='<?php echo $authUrl; ?>'>Connect Me!</a>
<?php else: ?> <?php echo "Access Allowed" ?>
<?php endif; ?>
</div>
<?php if (isset($result) && $result): ?>
<div class="shortened">
<?php echo "aaa"; ?>
<?php echo $result->webContentLink; ?>
<?php echo "bbb"; ?>
</div>
<?php endif ?>
<?php if($pdf_filename==="") ?>
<?php echo "This is for GDrive Upload from MiniROI" ?>
</div>

Yii2: How to display Multiple images in view from path saved in database?

I have column called images in database which contain image paths.
Query:
$query = new Query;
$todo = (new yii\db\Query())
->select(['images'])
->from('room_types')
->andWhere("id = '$model->id'")
->all();
View :
<?php
foreach ($todo as $row)
{
?>
<?php echo Yii::getAlias('#web').'/'.$row; ?>
<?php
}
?>
Images path saved in db:
uploads/room_img/30.jpg;uploads/room_img/300.jpg;uploads/room_img/11928_569674493052762_732198968_n.jpg;
Tried with explode():
<?php
function room_images() {
$query = mysql_query("SELECT images FROM room_types WHERE id = $model->id");
while($row = mysql_fetch_array($query)) {
$e[] = explode(" ", $row[0]);
foreach($e as $r) {
echo $r;
}
}
}
?>
But nothing is showing
Use img method of Html class - reference
In your view file
use yii\helpers\Html;
// ...
<?php foreach ($todo as $key=>$row): ?>
<!-- html code if you need -->
<?php
foreach (explode(';', $row['images']) as $key_img => $value_img)
{
echo Html::img(Yii::getAlias('#web').'/'.$value_img);
}
?>
<!-- html code if you need -->
<?php endforeach; ?>
// ...

Send variable to next page without overwriting

I want to pass $ids variable retrieved from db from page1 to page2 with session method but the code doesn't work or overwrites the variable.
page1
<?php
$queryString = "WHERE id='$id'";
$sqls = mysql_query("SELECT ids FROM markers $queryString");
$i=0;
while($row=mysql_fetch_array($sqls)) {
$_SESSION['ids'][$i]=$row['ids'];
echo $_SESSION['ids'][$i];
echo 'Modify<br />';
$i++;
}
//echo $_SESSION['ids'];
?>
page2
<?php
session_start();
include_once "scripts/connect_to_mysql.php";
$ids= $_SESSION['ids'].[$i];
//echo $ids;
?>
Thanks a lot for help.
you must do also session_start(); in page1 to work with sesion variables.
your code should work like that:
while($row=mysql_fetch_array($sqls)) {
$arrays[]=$row['ids'];
$_SESSION['ids']= $arrays[];
echo $_SESSION['ids'][0];
echo $_SESSION['ids'][1];
.......
echo 'Modify<br />';
}
page2
<?php
session_start();
include_once "scripts/connect_to_mysql.php";
$ids= $_SESSION['ids'];
echo $ids[0];
echo $ids[1];......
?>