I have this structure on my page:
*{
margin: 0px;
}
.div1{
width: 1000px;
background-color: grey;
overflow: hidden;
}
.div2{
width: 300px;
background-color: orange;
height: 500px
}
.div3{
width: 300px;
background-color: orange;
height: 500px;
}
.float_left{
float: left;
}
.float_right{
float: right;
}
<div class="div1">
<div class="div2 float_left">
</div>
<div class="div3 float_right">
</div>
</div>
And inside the two orange container's I want to put some smaller divs, but I read the data from a database. The first row should be in div1, the second in div2, the third in div1 and so on.
$sql = "SELECT * FROM question ORDER BY question_id DESC";
$result = mysqli_query($db,$sql);
while($row = mysqli_fetch_assoc($result)){
}
But how can I do something like that? Can I open a div container closed container to place a div container inside the container?
You should really read and try by yourself, this is pretty basic question.
There are many ways, here is one of them.
Declare 2 variables to store the results for Div1 and Div2.
Declare a count variable and use the odd and even property to decide who turn is it to store the results.
Output the results.
PHP:
$sql = "SELECT * FROM question ORDER BY question_id DESC";
$result = mysqli_query($db,$sql);
$resultsForDiv1 = "";
$resultsForDiv2 = "";
$count = 0;
while($row = mysqli_fetch_assoc($result)){
if ($count%2 == 0) {
$resultsForDiv1 .= $row[0]; // You should change it to whatever data you need from $row.
}
else {
$resultsForDiv2 .= $row[0]; // You should change it to whatever data you need from $row.
}
$count++;
}
Html:
<div class="div1">
<div class="div2 float_left">
<?php echo $resultsForDiv1; ?>
</div>
<div class="div3 float_right">
<?php echo $resultsForDiv2; ?>
</div>
</div>
What you should do is store the data from the $row variable into new variables which you can then output in the two columns. Like this:
<?php
$sql = "SELECT * FROM question ORDER BY question_id DESC";
$result = mysqli_query($db, $sql);
// Use this to toggle column on the sql results
$left_right = true;
while ($row = mysqli_fetch_assoc($result)) {
if ($left_right) {
// Add to left column variable
// [some code to add the data from $row to $left_column_content]
} else {
// Add to right column variable
// [some code to add the data from $row to $right_column_content]
}
// Switch columns by inverting the boolean from true to false, false to true
$left_right = !$left_right;
}
?>
<div class="div1">
<div class="div2 float_left">
<?php echo $left_column_content; ?>
</div>
<div class="div3 float_right">
<?php echo right_column_content; ?>
</div>
</div>
Related
Having a bit of an issue here. Could use some insight. I'm displaying user created events to visitors to my website. I'm using a while loop to display everything that hasn't not yet already passed. My goal is to display two separate events right next to each other, then another two below that and so on. Currently I'm using the flex box css property to achieve this, but it's only displaying the output vertically and not the way I want it to, meaning it's only putting one event per line. Here is my current output for displaying the events.
include 'db_connect.php';
$event_type = $_POST['event_type'];
$state = $_POST['state'];
$current_date = date("Y-m-d");
$sql = "SELECT * FROM events WHERE event_type LIKE '%".$event_type."%' AND state LIKE '%".$state."%' AND end_date > '$current_date' ORDER By end_date ASC";
$result = mysqli_query($conn, $sql);
if (isset($_POST['search-events']) && !empty($event_type) && !empty($state)) {
while($row = mysqli_fetch_array($result)) {
$event_name= $row['event_name'];
$image = $row['image'];
$start_date = $row['start_date'];
$end_date = $row['end_date'];
$start_time = $row['start_time'];
$end_time = $row['end_time'];
$street = $row['street'];
$city = $row['city'];
$state = $row['state'];
$zip_code = $row['zip_code'];
$id = $row['event_id'];
echo '<div class="filter-wrap">';
echo '<div id="filter-boxes">';
echo '<div id="list_image"><img src="'.$image.'"></div>';
echo '<div id="list_name">'.$event_name.'</div>';
echo '<div id="list_date">'.$start_date. ' - ' .$end_date. '</div>';
echo '<div id="list_time">' .$start_time. ' - ' .$end_time. '</div>';
echo '<div id="list_location">'.$street.''.$city.''.$state.''.$zip_code.'</div>';
echo '</div>';
echo '</div>';
}
}
Then there's the css that I'm using.
.filter-wrap {
display: flex;
flex-direction: row;
padding: 10px;
justify-content: center;
align-items: center;
}
#filter-boxes {
border: 2px solid #999;
text-align: center;
width: 50%;
height: 150px;
}
As you can see, I'm using the flex property inside the container that holds each of the individual boxes that holds each event. I have the flex direction set to row since I want it to display horizontally, then go the next line after it runs out of room on each line.
I tried a few things. I tried switching to the css column count property but didn't get the results I was expecting. I honestly probably didn't tweak with that property enough, but I have my heart set on the flex box property. I also tried setting the flex direction to column and also tried adding an inline-block display property to the boxes that are suppose to repeat on the while loop with each event. I'm finding this online that are kind of similar to my issue, but not quite. One uses javascript, but this can obviously also be accomplished somehow with php. I also found several articles talking about centering the content using flexbox, which is not the goal here.
Try move your .filter-wrap div element to outside of the while() {} loop.
Your current coding:
while() {
echo '<div class="filter-wrap">';
echo '<div id="filter-boxes">';
// content goes here...
echo '</div>';
echo '</div>';
}
will result in following structure where each .filter-wrap only container a single child .filter-boxes, which will always results in vertical presentation:
<div class="filter-wrap">
<div id="filter-boxes"> content </div>
</div>
<div class="filter-wrap">
<div id="filter-boxes"> content </div>
</div>
<div class="filter-wrap">
<div id="filter-boxes"> content </div>
</div>
For horizontal presentation, the correct structure should be one .filter-wrap consists of multiple .filter-boxes childs:
<div class="filter-wrap">
<div id="filter-boxes"> content </div>
<div id="filter-boxes"> content </div>
<div id="filter-boxes"> content </div>
</div>
So you can try change your coding to:
echo '<div class="filter-wrap">';
while() {
echo '<div id="filter-boxes">';
// content goes here...
echo '</div>';
}
echo '</div>';
Snippet for demo to you the coding logic result. It is in JS but you just have to apply the same in your PHP
Hope it helps and Happy coding!
var result_1 = document.getElementById('result_1');
var result_2 = document.getElementById('result_2');
var content_1 = '';
var content_2 = '';
content_2 = '<div class="filter-wrap">';
for (var i = 1; i <= 5; i++)
{
// result 1
content_1 += '<div class="filter-wrap"> <div class="filter-boxes">content ' + i + '</div> </div>';
// result 2
content_2 += '<div class="filter-boxes">content ' + i + '</div>';
}
content_2 += '</div>';
result_1.insertAdjacentHTML('beforeend', content_1);
result_2.insertAdjacentHTML('beforeend', content_2);
.filter-wrap {
display: flex;
flex-direction: row;
flex-wrap: wrap;
padding: 10px;
justify-content: center;
align-items: center;
}
.filter-boxes {
border: 2px solid #999;
text-align: center;
width: 50%;
height: 50px;
/* for two columns display */
max-width: 49%;
}
#result_1,
#result_2 {
background-color: lightgray;
border: 1px dashed black;
margin: 3px;
}
result 1
<div id="result_1"></div>
result 2
<div id="result_2"></div>
I want my page to automatically redirect to another page when the data is fetch on the page so how can I do it ? i have this code
here is my code here is where the data is fetched but once it fetch i want to redirect it automatically.
<div>
<?php
$data = mysqli_query($con,"SELECT * FROM announcement");
$count = mysqli_num_rows($data);
if ($count != 0) {
//IVE ASSUMED THAT THIS WILL WORK BUT IT DIDNT
header("location:../addRedirect.php");
while($row = mysqli_fetch_array($data)) {
echo '<div id="myModal" class="modal" style="position:fixed; display: none; padding-top: 100px;
left: 0; top: 0; width: 100%; height: 100%; overflow: auto; background-color: rgb(0,0,0); background-color: rgba(0,0,0,0.9); ">
<div class="container" style="width: 100%">
<div class="card col-12" style="background-color: red; color: white;">
<div class="card-header">
<p><h3 class="text-center">Announcement</h3></p>
</div>
<div class="card-body text-center">
<h5>'.$row['additional_info'].'</h5>
<p>Please click refresh button to resume after the announcement</p>
</div>
</div>
</div>
<img class="modal-content" id="img01">
</div>';
$dataid = $row['id'];
}
}
?>
</div>
and on my second page i want to redirect it back.. so what i have is an auto refresh when im just fetching data.. thanks guide me pls this is a huge help for me as a starter of php and codings thanks.
<?php
header("location:../home.php");
?>
You can refresh your page using javascript instead of PHP, if the headers are already set.
<div>
<?php
$data = mysqli_query($con,"SELECT * FROM announcement");
$count = mysqli_num_rows($data);
if ($count != 0) {
echo "<script>setTimeout(\"location.reload()\",1000);</script>";
// Other stuff ...
}
?>
</div>
If you want to navigate to an other page you can do this:
<div>
<?php
$data = mysqli_query($con,"SELECT * FROM announcement");
$count = mysqli_num_rows($data);
if ($count != 0) {
echo "<script>setTimeout(\"location.href = '../addRedirect.php';\",1000);</script>";
// Other stuff ...
}
?>
</div>
Both examples have a delay of 1 second (1000 milliseconds)
I have a problem. I have two tables in my database which I want to write as an output (their names) but it always writes only the first one in the database. Can anybody fix my code and explain why that fix is necessary?
$result = mysqli_query($conn, "SHOW TABLES");
while($tableName = mysqli_fetch_array($result))
{
$table = $tableName[0];
$result2 = mysqli_query($conn, "SELECT * FROM `".$table."`");
$query = "SELECT COUNT(*) FROM `".$table."`";
$result = mysqli_query($conn, $query);
$rows = mysqli_fetch_row($result);
$querys = "SELECT * FROM `".$table."` ORDER BY id DESC LIMIT 1";
$resulty = mysqli_query($conn, $querys);
$rowsy = mysqli_fetch_row($resulty);
while($row2 = mysqli_fetch_assoc($result2))
{
$p = $row2['authoroftopic'];
$s = $rows[0];
$l = $rowsy[1];
echo "<div class='discussionTable'>
<div class='dcolumn' style='width: 60%; position: absolute; padding-left: 10px;'>
<b><h3 style='padding: 15px;'><a href='discussion_gaming.php' style='color: white'>$table</a></b><br><p style='font-size: 13px'>Author: $p</p></h3>
</div>
<div style='padding-top: 10px;'>
<div class='dcolumn' style='width: 30%; float: right;'>
<p style='padding: 15px; line-height: 0px'>Total comments: $s</p>
<p style='padding-left: 15px; padding-top: 0px; line-height: 0px'>Last comment by: $l</p>
</div>
</div>
</div>";
}
}
You use the variable $result for two result sets at the same time - the first to hold the list of available tables, and the second one to gather data from a single table. You should rename one of the two occurences to not override the outer result within the loop
i have a div and also on same page i have some data which is show on web page i just want data show on div how can i do this? i am very very new in php .thanks here is the php file.
<?php
define('HOST','xxxxxxxxxxx');
define('USER','xxxxxxxxxxxxx');
define('PASS','xxxxxxxx');
define('DB','androidapi2');
$con = mysqli_connect(HOST,USER,PASS,DB);
$sql = "select * from users WHERE status = 1";
$res = mysqli_query($con,$sql);
$result = array();
while($row = mysqli_fetch_array($res)){
array_push($result,
array(
// 'id'=>$row[0],
'email'=>$row[3],
));
}
echo json_encode(array("result"=>$result));
mysqli_close($con);
?>
<html>
<head>
<title>DIC ChatBox Beta 1</title>
<style>
#usersOnLine {
font-family:tahoma;
font-size:12px;
color:black;
border: 3px teal solid;
height: 525px;
width: 250px;
float: right;
overflow-y:scroll;
}
.container{
width:970px;
height:auto;
margin:0 auto;
}
</style>
</head>
<body>
<div class="container">
</div>
<h2> all contacts</h2>
<div id="usersOnLine">
</div>
</div>
</body>
</html>
here is the div id usersonline where i want all data show . how to made it.
There is no need to use json_encode(array("result"=>$result));, directly use $result
<div id="usersOnLine">
<?php
foreach($result as $key => $val)
{
echo $val['email'];
echo "<br>";
}
?>
</div>
If you want to use it on another page
Creating New Session In php file
<?php
session_start(); ///at the top of this file
///after your query and creating array
$_SESSION["result"]= $result;////set $result in session
?>
Getting Session on another page
<?php
session_start();
if(isset($_SESSION["result"]))
{
$result = $_SESSION["result"];
///your foreach loop to print data
}
?>
READ SESSION DOCUMENTAION
Here's what I'm basically trying to do:
Retrieve the values of a column
Store those values into an array in PHP
Echo each value with a line break in between each value
Here's my attempted code:
<?php
$connection = mysqli_connect("localhost", "root", "bruhfrogzombie098", "growtapi_social");
if (!$connection) {
die("Failed to connect to MYSQL: " . mysqli_connect_errno());
};
$members = mysqli_query($connection, "SELECT Username FROM s_users");
$members_status = mysqli_query($connection, "SELECT Status_Content FROM s_users");
$members_array = array();
while ($member = mysqli_fetch_assoc($members)) {
$members_array[] = $member;
};
$members_status_array = array();
while ($status = mysqli_fetch_assoc($members_status)) {
$members_status_array[] = $status;
};
?>
And this is where I want to echo out the values:
<center>
<h1>Members Directory</h1>
<div style="width: 20%; height; 75%; border: 3px solid black; margin: auto; overflow: hidden; overflow-y: scroll;">
<?php echo $members_array['$member'];
echo "<br />";
?>
</div>
</center>
I don't receive any errors, but the problem is that nothing shows up in the div, meaning that I either didn't retrieve the data properly or didn't use it right.
Note: I've finally moved on to writing up-to-date code, so I hope no one here comments that somewhere in this code I have outdated code ( ͡° ͜ʖ ͡°)
$members_array = array();
while ($member = mysqli_fetch_assoc($members)) {
$members_array[] = $member;
//$members_array is array and $member is array so $members_array like $members_array[][];
};
<center>
<h1>Members Directory</h1>
<div style="width: 20%; height; 75%; border: 3px solid black; margin: auto; overflow: hidden; overflow-y: scroll;">
<?
$member_count = count($members_array);
for( $i = 0 ; $i < $member_count ; $i++ ){
echo $member_array[$i]['Username'];
echo "<br />";
}
?>
</div>
</center>