Create php $_SESSION from count variable - php

I am trying to create a $_SESSION from example code count($result). I have decalred the variable like thus:
$result = array();
$result[] = $boxdest;
This is working fine on the page where I run it but I need to pass the value to a page to use in php mail() function. I have tried
$_SESSION['result'] = $result;
But all I get is Resource id #40. How do I pass this variable to another page. Full code example here: http://sandbox.onlinephpfunctions.com/code/ba0a34774e30c93edfdf02f531bb199c681021e3 Many thanks

This is more of a critique of the code rather than a solution to the problem:
$_SESSION['result'] = []; //Clear it in case there's an old value here.
if (isset($_POST['boxdest']) && is_array($_POST['boxdest']) && !empty($_POST['boxdest'])) { //Merged condition
$destroydata = explode(',', $_POST['boxdest'][0]); //Split was deprecated
$result = array_filter($destroydata);
if (empty($result) {
$boxdesterror = '<span style="font-weight:bold;font-size:12px;color: #ff0000;">' . 'BOX DESTRUCTION ERROR! ' . '</span>' . '<span style="font-weight:normal;color: #000;background-color:#ffa;">' . 'You must enter a box for destruction' . '</span>';
echo "<script language=\"JavaScript\">\n";
echo 'alert("BOX DESTRUCTION ERROR:\nYou must enter a box for destruction.");';
echo "</script>";
echo $boxdesterror;
} else {
echo 'You wish to destroy ' . count($result) . ' box(es): ' . '<div style="word-wrap:break-word;white-space: pre-wrap;overflow:auto !important;height: 100px; width: 250px; border: 1px solid #666; background-color: #fff; padding: 4px;">' . '<span style="font-weight:bold;color: #000;">' . implode(', ', $boxdest) . '</span>' . '</div>' . '<p />';
$_SESSION['result'] = $result; //Did you really need both?
$flag = 1;
}
}
This should be the same results. I don't see why a resource would pop up from nowhere though.

U can't store any Resource in PHP session
https://stackoverflow.com/a/42389037/5361130
http://php.net/manual/en/function.serialize.php
EDIT
if your $result is result of function like mysqli_query or something like this, use mysqli_num_rows to get number of rows not count
http://php.net/manual/en/mysqli-result.num-rows.php
$result = mysqli_num_rows($boxdest)
or
$_SESSION['result'] = mysqli_num_rows($boxdest)

Related

How do I style my text inside an echo?

Part of the code is
while ($row = mysqli_fetch_assoc($result))
{
echo "$row[firstname] $row[lastname]";
echo "<hr>";
echo "<strong>Date Referred:</strong>     $row[cf_831]";
echo "<br/>";
echo "<strong>Date Today:</strong>       $today";
echo "<br/>";
}
How do I style this part for example?
"$row[firstname] $row[lastname]"
You can try
echo '<p class="mystyle">' . $row['firstname'] . " " . $row['lastname'] . '</p>';
Then add CSS
.mystyle {
}
Addition to Sidsec9's answer: if you are afraid of stylesheets, you can also consider:
echo '<p style="color: red; font-size: 10pt;">' . $row['firstname'] . " " . $row['lastname'] . '</p>';
You could also use styles to get rid of  , for example using margin-right or padding-right properties.
echo '<p id="name"> '. $row[firstname] $row[lastname] .'</p>';
Css you only using for name alone use id else use class and use the single css for all values.
#name
{
color: red;
}
You can wrap it in a class, and fix it with a stylesheet. I also changed the echo, you're allowed to close the PHP tag, do some HTML and then open PHP again.
<?=$variable?> is short echo, it is the same as <?php echo $variable; ?>.
while ($row = mysqli_fetch_assoc($result)){
?>
<span class="Example"><?=$row[firstname].' '.$row[lastname]?></span>
<hr>
<strong>Date Referred:</strong>     <?=$row[cf_831]?>
<br/>
<strong>Date Today:</strong>       <?=$today?>
<br/>
<?php
}
A better method would be making and html file, say example.com, and place <!-- FIRSTNAME -->,<!-- LASTNAME --> AND <!-- TODAY --> instead of the variables. Then, using php:
$totalResult = "";
$template_one = file_get_contents("example.html");
while ($row = mysqli_fetch_assoc($result)){
$item = $template_one;
$item = str_replace("<!-- FIRSTNAME -->", $row['firstname'], $item);
$item = str_replace("<!-- LASTNAME -->", $row['lastname'], $item);
$totalResult.= $item; // add to totel result
)
// $today doesnt change in the loop, do it once after the loop is done:
$totalResult = str_replace("<!-- TODAY -->", $today, $totalResult);
echo $totalResult; // Now you have the result in a pretty variable

Loop through database for a particular record and show results - PHP

i'm looking for a way to loop through the database and shows results in a html page, i'm actually creating a sorta fake email system for a little project.
so i have a table name "SENDS" with 5 columns ID, Mailtext, Touser, Subject, Fromuser.
what i wanna do is when user log in system connects to the database and select everything
from SENDS where touser='$email' -$email is defined when user logs in- and display it in a html div
<div class="oubx">
<div class="inbx">
<span class="from"></span>
<span class="subject"></span>
<span class="mailtext"></span>
</div>
</div>
.oubx {
height:27px; width:800px;
border:1px solid black;
margin-top:;
margin-left:80px;
float:left;
background-color:white;
}
.inbx {
height:22px; width:700px;
border:1px solid black;
margin-top:1px;
margin-left:45px;
background-color:white;
font-family:sans-serif;
font-weight:bold;
font-size:16px;
color:#808080;
float:left;
}
from of course tells whos it from
subject is subject
mailtext is what the mail is (it's hidden of course jQ will take care of it once clicked)
i have used mysqli_num_rows, mysqli_fetch_array, for loops, foreach etc but i just can't seem to get my head around it i'm having a hard time figuring out how to do it setp by steps or what the step is. please help :) thanks.
$connectDB = mysqli_connect("host","root","","db");
$touser = mysqli_query($connectDB, "select * from sends where touser='$email'");
$to_numrows = mysqli_num_rows($touser);
$to_fetch = mysqli_fetch_array($touser);
//printing of html code = how many rows
$ib = '<div class="oubx"><div class="inbx"><span class="from">'.$value.'</span><span class="subject">'.$value.'</span></div></div>';
for ($i = 0; $i < $to_numrows; $i++) {
echo $ib;
}
the rest is work in progress..
Try this,
$connectDB = mysqli_connect("host","root","","db");
$touser = mysqli_query($connectDB, "select * from sends where touser = '$email'");
$all_rows = mysqli_fetch_all($touser, MYSQLI_ASSOC);
$ib = '';
foreach($all_rows as $row) {
$ib .= '<div class="oubx">'
. '<div class="inbx">'
. '<span class="from">' . $row['Fromuser'] . '</span>'
. '<span class="subject">' . $row['Subject'] . '</span>'
. '</div>'
. '</div>';
}
echo $ib;
1- mysqli_fetch_all will fetch all result row into a PHP array
2- We loop through it with foreach (no need to know the rows count)
3- Foreach row, we append the HTML to the $ib intially set to empty string.
Hope it helps.
put the query result in a variable:
while ($row = $touser->fetch_array()) {
$rows[] = $row;
}
and iterate it with a loop:
foreach ($rows as $row) {
/* show appropriate html here */
}

Display number of files in the folder using PHP

I've this simple script for displaying all the images in the folder.
<?php
foreach(glob("".$filePath."/*.{jpg,JPG,jpeg,JPEG,gif,GIF,png,PNG}",GLOB_BRACE) as $images)
{
$filecount = count(glob("".$filePath."/*.{jpg,JPG,jpeg,JPEG,gif,GIF,png,PNG}",GLOB_BRACE));
if ($filecount >1)
{
echo "<img width='75' height='auto' style='margin-right: 3px; border:1px solid #dddddd' alt='".$row["caption"] ."' src=\"".$images."\">";
}
else
{
echo "<img width='200' height='auto' style='margin-right: 3px; border:1px solid #dddddd' alt='".$row["caption"] ."' src=\"".$images."\">";
}
}
?>
I want to display number of files in the folder.Giving below what i tried..My problem is that this shows the number of files before each image.
if ($filecount >1)
{
echo '' . $user . ' ' .'added ' . ' ' . $filecount . ' ' . 'new photos';
echo "<img width='75' height='auto' style='margin-right: 3px; alt='".$row["caption"] ."' src=\"".$images."\">";
}
How can I show number of files before the group of images displayed?
You need to display the number of files only after it has collected them.
You can do it by declaring the filecount variable before the loop, then you need to count and sum the value everytime the loop goes to the next result, and eventually display the total number of files.
Here is how the code should be written:
<?php
$filecount = 0;
foreach(glob("".$filePath."/*.{jpg,JPG,jpeg,JPEG,gif,GIF,png,PNG}",GLOB_BRACE) as $images)
{
$tempFileCount = count(glob("".$filePath."/*.{jpg,JPG,jpeg,JPEG,gif,GIF,png,PNG}",GLOB_BRACE))
$filecount += $tempFileCount;
if ($tempFileCount > 1)
{
echo "<img width='75' height='auto' style='margin-right: 3px; border:1px solid #dddddd' alt='".$row["caption"] ."' src=\"".$images."\">";
}
else
{
echo "<img width='200' height='auto' style='margin-right: 3px; border:1px solid #dddddd' alt='".$row["caption"] ."' src=\"".$images."\">";
}
}
if($filecount > 1)
{
echo '' . $user . ' ' .'added ' . ' ' . $filecount . ' ' . 'new photos';
}
?>
First create a function that returns all images as array. This will throw an exception if there is an error with the glob php function (will mostly never happen, but worth it to know it when it happens). The function will give the sense that the code is a little bit cleaner.
<?php
function getAllImagesOnDirectory($directory) {
$imagesArray = glob("".$filePath."/*.{jpg,JPG,jpeg,JPEG,gif,GIF,png,PNG}",GLOB_BRACE);
if(!is_array($imagesArray)) {
throw new RuntimeException("There is a problem getting images on directory " . $directory);
}
return $imagesArray;
}
//Then we call this function to get all the images.
$allImagesOnFilePath = getAllImagesOnDirectory($filePath);
$numberOfImages = count($allImagesOnFilePath);
if($numberOfImages > 0) {
echo '<p>' . $user . ' added ' . $numberOfImages . ' new photos</p>';
//If the number of images is more than 0 then traverse all the images and echo them
foreach($allImagesOnFilePath as $oneImage) {
echo "<img style='max-width:100%;height:auto;margin-right: 3px; border:1px solid #dddddd' alt='".$row["caption"] ."' src=\"".$oneImage."\">";
}
}
else {
//The user has no uploaded images
echo '<p>' . $user . ' has not added new photos yet :(</p>';
}
This way we can use the $numberOfImages before using foreach and thus doing what you requested.
We also here use the style attribute to apply width and height to images. See this article to see how to make autoresizable images: http://unstoppablerobotninja.com/entry/fluid-images

dynamically divs creation on the basis of table records

I have a page in which i have to display a list of job boards. I have a table named as job_board in which i all the record of 10 to 12 may be more job boards is saved. I want to display a page in which there will be a div as container and inside that div i want to display these job boards, each in a separate div(side by side). At the moment i am trying this loop:
foreach($this->test as $value)
{
$variable=null;
echo $variable .='<div class="testing"></div><div class="head"></div>';
$variable .=$value["job_board_id"] . '</br>';
$variable .=$value["name"] . '</br>';
$variable .=$value["description"] . '</br>';
$variable .=$value["price"]. '</br>';
$variable .= $value["enabled"]. '</br>';
echo $variable;
}
The columns in my table are id,name,description,price,url)
I want to display it in the small divs which are inside the main div "container"
If you want show divs side by side, please try float. The code may goes like this:
foreach($this->test as $value)
{
$variable=null;
$variable .='<div class="cell">';
$variable .=$value["job_board_id"] . '</br>';
$variable .=$value["name"] . '</br>';
$variable .=$value["description"] . '</br>';
$variable .=$value["price"]. '</br>';
$variable .= $value["enabled"]. '</br>';
$variable .='</div>';
echo $variable;
}
And, the class 'cell' will be defined in your CSS file like below:
<style type="text/css">
.cell{
width:150px;
float:left;
}
#container:after { /*clear the float*/
visibility: hidden;
display: block;
font-size: 0;
content: " ";
clear: both;
height: 0;
}
</style>
if you wish to print each line in one div with span you can try with this
echo $variable .='<div class="testing"></div><div class="head"></div>';
$variable .= '<div><span>'.$value["job_board_id"] . '</span>';
$variable .= '<span>'.$value["name"] . '</span>';
$variable .= '<span>'.$value["description"] . '</span>';
$variable .= '<span>'.$value["price"]. '</span>';
$variable .= '<span>'.$value["enabled"]. '</span></div>';
echo $variable;
And for second loop after print $variable please assign with blank like $variable = '';
Also try to make more attractive with css.
let me know if i can help you further.

PHP looping problem?

I'm trying to display the first row in one color and the second row in another color but my code displays the result twice in both colors for example lets say I have 5 results my code will double the results by displaying 10 results. How can I fix this problem?
Here is the php code.
while ($row = mysqli_fetch_assoc($dbc)) {
//first row
echo '<h3 class="title">' . $row['title'] .'</h3>';
echo '<div class="summary">' . substr($row['content'],0,255) . '</div>';
//second row
echo '<h3 class="title-2">' . $row['title'] .'</h3>';
echo '<div class="summary-2">' . substr($row['content'],0,255) . '</div>';
}
You need to change the class on each row:
$count = 0;
while ($row = mysqli_fetch_assoc($dbc)) {
if( $count % 2 == 0 ) {
$classMod = '';
} else {
$classMod = '-2';
}
//first row
echo '<h3 class="title' . $classMod . '">' . $row['title'] .'</h3>';
echo '<div class="summary' . $classMod . '">' . substr($row['content'],0,255) . '</div>';
$count++;
}
your code should be like this
CSS
.odd { background: #CCC }
.event { background: #666 }
PHP
$c = true;
while ($row = mysqli_fetch_assoc($dbc)) {
$style = (($c = !$c)?' odd':' even');
echo '<h3 class="title '.$style.'">' . $row['title'] .'</h3>';
echo '<div class="summary '.$style.'">' .substr($row['content'],0,255) . '</div>';
}
Here's a solution with minimal repetition:
$count = 0;
while (($row = mysqli_fetch_assoc($dbc)) && ++$count) {
printf(
'<h3 class="title%1$s">%2$s</h3>'
. '<div class="summary%1$s">%3$s</div>'
, $count % 2 ? "" : "-2"
, $row['title'] // might want to use htmlentities() here...
, substr($row['content'], 0, 255) // and here...
);
}
$i = 0;
while ($row = mysqli_fetch_assoc($dbc)) {
$color =$i % 2;
echo '<h3 class="title-' .$color . '">' . $row['title'] .'</h3>';
echo '<div class="summary">' . substr($row['content'],0,255) . '</div>';
$i++;
}
Your code just displays every result twice. Use a conditional (e.g. an integer or a boolean) to switch between rows (like: if true, then green; if false, then red).
For a boolean you could change the current value like so:
bool = !bool;
Couple of extra points:
You don't (normally) need so many classes. If you have <div class="stripe"> as your container, you can target the items with e.g. .stripe h3 in CSS.
If you target the odd and even items in CSS with .stripe h3, you can then overwrite just the odd items.
In a perfect world, you should keep presentation in the CSS. All browsers but IE7 and below support div:odd to target any odd child of a parent. This may require changing the structure of your HTML slightly. For IE7 and below, I'd add classes with JavaScript instead of PHP. When IE7 is no more then you can just remove the JS.
By the way, you could do this code too:
while ($row = mysqli_fetch_assoc($dbc)) {
echo '<h3 class="title">' . $row['title'] .'</h3>';
echo '<div class="summary">' . substr($row['content'],0,255) . '</div>';
if($row = mysqli_fetch_assoc($dbc)){
echo '<h3 class="title-2">' . $row['title'] .'</h3>';
echo '<div class="summary-2">' . substr($row['content'],0,255) . '</div>';
}
}
IMO, there is no excuse for not delegating this kind of non-critical, presentation layer decoration to client side code.
Just use a library like jQuery and access the odd and even rows like so:
<script>
$(document).ready(function()
{
//for your table rows
$("tr:even").css("background-color", "#F4F4F0");
$("tr:odd").css("background-color", "#EFF1F2");
});
</script>
You'll have us generating font tags next.

Categories