How to embed a php function within an echo? - php

I want to list blogs with user membership on user home page as or whatever possible tag. yet I want every listed blog to launch a divert() function onClick. This function should assign a new $_SESSION[] variable, and then redirect the user to the blog.php (blog page). In the code below, divert() function launch once I call the home page.
include_once("inc/dbconnect.php");
session_start();
function divert($x) {
$_SESSION['blnm'] = $x;
header("Location: blog.php");
}
function listing() {
global $conn;
$member = $_SESSION['u'];
$outquery = "SELECT * FROM blogmembership WHERE blogmember='$member'";
$result = mysqli_query($conn, $outquery);
$no = mysqli_num_rows($result);
while ($row = mysqli_fetch_array($result)) {
$list = $row['blogname'];
echo "<li><a onClick='".divert($list)."' id='".$list."'>".$list."</a> </li><br />";
//How to launch the "divert()" function onClick only?
}
}

Related

How can I use the value of variables defined inside a php class in another page

I'm fairly new using PHP classes, I have done some PHP using the procedural method for a while but never fully done proper object oriented programming on php. Which brings me to my question:
I have one file called classes.php where I intend to define all my php classes. Inside that file I have created a class as follows:
require_once("connection.php");
class User
{
public function getUserInfo() {
$userinfo = mysqli_query($con,"SELECT * FROM users WHERE username='". $_SESSION['username'] ."'");
while($row = mysqli_fetch_assoc($userinfo)) {
// USER DETAILS:
$userid = $row['id'];
$username = $row['username'];
$userfirstname = $row['first_name'];
$userlastname = $row['last_name'];
$useremail = $row['email'];
$useraddress1 = $row['address_line_1'];
$useraddress2 = $row['address_line_2'];
$userpostcode = $row['postcode'];
$userphone = $row['phone'];
$usermobile = $row['mobile'];
$usercreated = $row['created'];
$usermodified = $row['modified'];
$useraccess = $row['access_level'];
}
}
} // end of User
Now I want to use the values of the variables defined in that class in another page. But how can I access those values?
So far I'm trying this (unsuccessfully):
<?php
include "php_includes/classes.php";
$user = new User();
?>
<div class="somediv">
<?php echo $user->getUserInfo($username) ?>
</div>
I have also tried:
<?php
include "php_includes/classes.php";
$user = new User();
?>
<div class="somediv">
<?php echo $user->$username ?>
</div>
Is there any way to echo those variables in another page so I can use the user information in, for example, profile pages?
Thanks in advance.
you should return the value from function.
require_once("connection.php");
class User
{
public function getUserInfo() {
$userinfo = mysqli_query($con,"SELECT * FROM users WHERE username='". $_SESSION['username'] ."'");
$row = mysqli_fetch_assoc($userinfo);
return row
}
}
and now only have to show the detail of user
<?php
include "php_includes/classes.php";
$user = new User();
?>
<div class="somediv">
<?php $user_detail=$user->getUserInfo(); ?>
<label>name:</label><br>
<?php echo $user_detail["first_name"]; ?>
</div>
Hence you can check whether row is null or not and whole array can be returned by this function of class any where this class included in file.
Before your function you need make PUBLIC VARIABLE like:
public $userid;
Then next in code in function you need to use:
$this->userid = $row['id'];
EDIT
Your calss need to be:
class User
{
public $userid;
public function getUserInfo() {
$userinfo = mysqli_query($con,"SELECT * FROM users WHERE username='". $_SESSION['username'] ."'");
while($row = mysqli_fetch_assoc($userinfo)) {
// USER DETAILS:
$this->$userid = $row['id'];
}
}
} // end of User
And you also need include this class file in other php page when you want show data.
you have to declare the variable as PUBLIC
class User
{
public function getUserInfo() {
$userinfo = mysqli_query($con,"SELECT * FROM users WHERE username='". $_SESSION['username'] ."'");
while($row = mysqli_fetch_assoc($userinfo)) {
// USER DETAILS:
public $userid = $row['id'];
}
}
}
For more information check here

Php, Mvc how to do a overlay?

I'm working in PHP, model view controller.
I run a action that is called getallmypictures where the result is shown on a view. When I click the result picture I run another action that get's all my related products to this picture, and that result it shown on another view file.
How can I get the second action result to be shown in a overlay on the same page as my pictures views?
This is my first action getallmypictures() when its execute:
public function GetAllPicturesAction(){
//if (isset($_GET['showAll'])) {
//if (isset($_SESSION['status']) == "customers_inloggad") {
$db = new PDO("mysql:host=localhost;dbname=fashionable", "root", "");
$stm1 = $db->prepare("MYSQL");
if ($stm1->execute()) {
$res = $stm1->fetchAll();
require_once './views/pictureInspo.php';
}
else {
header("location:../start/index?msgWrong=empty");
}
}
and the other action getthispicture() is required to this PHP file to show the result:
public function GetThisPictureAction(){
if(isset($_GET['getPictureValue'])){
// if (isset($_SESSION['status']) == "customers_inloggad") {
$db = new PDO("mysql:host=localhost;dbname=fashionable", "root","");
$stm2 = $db->prepare("MYSQL");
$stm=$db->prepare("MYSQL");
$stm->bindParam(":picture_inspo_id", $_GET['getPictureValue']);
$stm2->bindParam(":picture_inspo_id",$_GET['getPictureValue']);
if ($stm2->execute()) {
$result = $stm2->fetchAll();
$stm->execute();
$result1 = $stm->fetchAll();
require_once './views/pictureInspo_overlay.php';
} }//}
else {
header("location:../start/index?msgWrong=empty");
}
}
instead of showing to actions on 2 views I would like to have a second view like an overlay but still keep them in different actions.
The routing im using is /controller/action

Catching the returned value

this may be a stupid question, but every source on the web seems not able to fully explain the logic to my complex brain
There's an edit page getting a $_GET['id'] from a link.
I got a function on my class elaborating this one to create an array of values from the database which must fill the form fields to edit datas. The short part of this code:
public function prel() {
$this->id= $_GET['id'];
}
public function EditDb () {
$connetti = new connessionedb();
$dbc = $connetti->Connessione();
$query = "SELECT * from anammi.anagrafica WHERE id = '$this->id'";
$mysqli = mysqli_query($dbc, $query);
if ($mysqli) {
$fetch = mysqli_fetch_assoc($mysqli);
return $fetch;
}
}
This array (which i tried to print) is perfectly ready to do what i'd like.
My pain starts when i need to pass it to the following function in the same class, which perhaps calls a parent method to print the form:
public function Associa() {
$a = $this->EditDb();
$this->old_id = $a['old_id'];
$this->cognome = $a['cognome'];
$this->nome = $a['nome'];
$this->sesso = $a['sesso'];
$this->tipo_socio_id = $a['tipo_socio_id'];
$this->titolo = $a['titolo']; }
public function Body() {
parent::Body();
}
How do i have to pass this $fetch?
My implementation:
<?php
require_once '/classes/class.ConnessioneDb.php';
require_once '/classes/class.editForm';
$edit = new EditForm();
$edit->prel();
if ($edit->EditDb()) {
$edit->Associa();
$edit->Body();
if (if ($edit->EditDb()) {
$edit->Associa();
$edit->Body();) {
$edit->Associa();
$edit->Body();
your Editdb method is returning a string and you are checking for a boolean condition in if statement. this is one problem.
using fetch-
$fetch=$edit->EditDb();
$edit->Associa();
$edit->Body($fetch);
Posting the full code of it:
public function prel() {
$this->id= $_GET['id'];
}
public function EditDb () {
$connetti = new connessionedb();
$dbc = $connetti->Connessione();
$query = "SELECT * from table WHERE id = '$this->id'";
$mysqli = mysqli_query($dbc, $query);
if ($mysqli) {
$fetch = mysqli_fetch_assoc($mysqli);
return $fetch;
}
}
public function Associa($fetch) {
$this->old_id = $fetch['old_id'];
$this->cognome = $fetch['cognome'];
$this->nome = $fetch['nome'];
$this->sesso = $fetch['sesso']; //it goes on from there with many similar lines
}
public function Body() {
$body = form::Body();
return $body;
}
Implementation
$edit = new EditForm();
$edit->prel();
$fetch=$edit->EditDb();
$edit->Associa($fetch);
$print = $edit->Body();
echo $print;
Being an edit form base on a parent insert form, i added an if in the parent form that sees if is set an $_GET['id] and prints the right form header with the right form action. This was tricky but really satisfying.

Query result data is displaying after page reload

This is my code.
index.php file:
$viewer_id = $_SESSION['viewer_id'];
$db = new Database('144.76.6.45','5432','3331','asd','31sd23');
$db->querySelect("SELECT * FROM users WHERE vk_id = $viewer_id");
$row = $db->STH->fetch();
if(empty($row)){
require 'template/default/not_logged.php';
}
else {
require 'template/default/logged.php';
}
This part is understandable. Connecting to database, doing query. If viewer's id is in db - logged.php if not - not_logged.php
Lets say viewers id is already in db.
logged.php file:
<div id="text-right">
<p>Игровой ник: <?php $user->getEuName(); ?></p>
<p>Cоциум: <?php $user->getSociety(); ?></p>
<p>Основная профессия: <?php $user->getMainProfession(); ?></p>
<p>Дополнительная информация: <?php $user->getNotes(); ?></p>
<p>Ищу команду для совместной охоты: <?php $user->getWantTeam(); ?></p>
<div class="edit-info">Редактировать</div>
</div>
This methods are getting info from db.
class User extends Database {
public $vk_id;
public $eu_name;
public $eu_society;
public $eu_notes;
public $eu_want_team;
public $eu_data;
function __construct() {
parent::__construct('144.76.6.45','5432','eu','eu','eu123');
}
function getEuName() {
$this->querySelect("SELECT eu_name FROM users WHERE vk_id = $this->viewer_id");
$row = $this->STH->fetch();
print '<b>'.$row['eu_name'].'</b>';
}
function getSociety() {
$this->querySelect("SELECT eu_society FROM users WHERE vk_id = $this->viewer_id");
$row = $this->STH->fetch();
print '<b>'.$row['eu_society'].'</b>';
}
function getNotes() {
$this->querySelect("SELECT eu_notes FROM users WHERE vk_id = $this->viewer_id");
$row = $this->STH->fetch();
print '<b>'.$row['eu_notes'].'</b>';
}
function getWantTeam() {
$this->querySelect("SELECT eu_want_team FROM users WHERE vk_id = $this->viewer_id");
$row = $this->STH->fetch();
if($row['eu_want_team'] == 'TRUE') {
print '<b>Да</b>';
}
else {
print '<b>Нет</b>';
}
}
function getMainProfession() {
$this->querySelect("SELECT eu_main_profession FROM users WHERE vk_id = $this->viewer_id");
$row = $this->STH->fetch();
print '<b>'.$row['eu_main_profession'].'</b>';
}
}
$user = new User();
And this is user class with get methods.(he is included in index.php like database.php(db connection class) is).
Everything is ok, but when viewer comes to page, the data of methods(query results) doesn't show, they show only AFTER RELOAD! I don't know whats the problem. Please solution.
Thanks!

Use jQuery/AJAX to get list of loggedin users from MySQL DB?

I have a site with a div that I want to list all currently logged in users. I set a bool in my DB where 1 means that users is logged in. I'm trying to dynamically generate the list using a jQuery AJAX call to a php page that polls the DB for all the logged in users.
The PHP code checks the DB and creates a list of all logged in users. My issue is I can't seem to get the jQuery function to load the data from the page.
<?php
include ('settings.php');
if ($db_found)
{
$sql = "SELECT * FROM $db_table WHERE logged_in='1'";
$result = mysql_query($sql);
$count = mysql_num_rows($result);
// Check if the account is already in the DB
$i = 0;
while ($i < $count)
{
$online = mysql_result($result,$i,"logged_in");
if ($online == 1)
{
$user = mysql_result($result,$i,"displayname");
print "<li>$user</li>";
}
$i++;
}
mysql_close($db_handle);
}
?>
Whats wrong with the way I used the jQuery load so the loggedin.php page content would be displayed in the #loggedin-players div?
<script type="text/javascript">
function updatePlayerList()
{
$('#loggedin-players').load('loggedin.php');
}
$setInterval("updatePlayerList()", 5000);
</script>
There should not be a $ before setInterval
setInterval("updatePlayerList()", 5000);
Your function is never called.

Categories