I've printed all the data from the database, but my main problem is how to design my data.
I have a table named post_tbl and columns(post_id,post_message,post_date)
this is my query:
$query = "SELECT `post_id`,`post_message` FROM `post_tbl` ORDER BY `post_date`;
This is how I print in php:
if($query_run = mysql_query($query))
{
while($query_row = mysql_fetch_assoc($query_run))
{
$ex_post_id = $query_row['post_id'];
$ex_post_message = $query_row['user_name'];
$ex_post_date= $query_row['post_date'];
echo $ex_post_message;
}
}
how do I make my ex_post_message have a unfirom border and width using html and css? pls help. thanks
if($query_run = mysql_query($query))
{
while($query_row = mysql_fetch_assoc($query_run))
{
$arrMaster[] = $ex_post_message;
}
}
foreach ($arrMaster as $key => $value)
{
if($i==0)
{
$table1.="<tr>";
foreach ($value as $keyc => $valuec)
{
$table1.="<th>".$keyc."</th>";
}
$table1.="</tr>";
$i=1;
}
$table1.="<tr>";
foreach ($value as $keyc => $valuec)
{
$table1.="<td>".$valuec."</td>";
}
$table1.="</tr>";
}
$table1 .= "</table>";
echo $table1;
at this way you can add any style to your table or any class
Yes you can echo HTML element like:
echo "<div class='classname1'>" . $ex_post_message . "</div>";
Then the class classname1 should handle the design. Like the following:
<style>
.classname1{
width: 100px;
height: 30px;
border: 1px solid blackl
}
</style>
There are many ways to achieve this. The one that I've provided is just an example.
Related
How can I create a dynamic menu created in a database?
A sample of the menu table :
ID NAME URL IDPARENT
----------------------------------
1 Accueil #Accueil 0
2 Parcs #Parcs 0
3 Allemagne #Allemagne 2
4 Berlin #Berlin 3
5 France #France 2
6 Contact #Contact 0
The result should be :
<ul>
<li>Accueil</li>
<li>Parcs</li>
<ul>
<li>Allemagne</li>
<ul>
<li>Berlin</li>
</ul>
<li>France</li>
</ul>
<li>Contact</li>
</ul>
SOLVED : My code :
<?php
//connection to the database
$dbhandle = mssql_connect('*****', '*****', '*****')
or die("Couldn't connect to Server");
//select a database to work with
$selected = mssql_select_db("*****", $dbhandle)
or die("Couldn't open database");
//declare the SQL statement that will query the database
$query = "SELECT * FROM CATEGORIES ";
//execute the SQL query and return records
$result = mssql_query($query);
//display the results
while($row = mssql_fetch_array($result))
{
// Assign by reference
$thisref = &$refs[ $row['ID'] ];
// add the the menu parent
$thisref['IDCategoriePere'] = $row['IDCategoriePere'];
$thisref['NOM'] = $row['NOM'];
$thisref['URL'] = $row['URL'];
// if there is no parent id
if ($row['IDCategoriePere'] == 0)
{
$list[ $row['ID'] ] = &$thisref;
}
else
{
$refs[ $row['IDCategoriePere'] ]['children'][ $row['ID'] ] = &$thisref;
}
}
function create_menu( $arr )
{
$html = "\n<ul>\n";
foreach ($arr as $key=>$val)
{
$html .= '<li>'.$val['NOM']."</li>\n";
if (array_key_exists('children', $val))
{
$html .= create_menu($val['children']);
}
}
$html .= "</ul>\n";
return $html;
}
echo create_menu( $list );
//close the connection
mssql_close($dbhandle);
?>
It works fine ! but when i tried to put css (http://cssmenumaker.com/menu/flat-jquery-responsive-menu), the dropdown doesn't show :(
Result with and witout css :
<style>
/* CSS Document */
#import url(http://fonts.googleapis.com/css?family=Open+Sans);
#import url(http://fonts.googleapis.com/css?family=Bree+Serif);
#container {
margin: 0 auto;
}
nav {
margin: 50px 0;
background-color: #E64A19;
}
nav ul {
padding: 0;
margin: 0;
list-style: none;
position: relative;
}
nav ul li {
display:inline-block;
background-color: #E64A19;
}
nav a {
display:block;
padding:0 10px;
color:#FFF;
font-size:20px;
line-height: 60px;
text-decoration:none;
}
nav a:hover {
background-color: #000000;
}
/* Hide Dropdowns by Default */
nav ul ul {
display: none;
position: absolute;
top: 60px; /* the height of the main nav */
}
/* Display Dropdowns on Hover */
nav ul li:hover > ul {
display:inherit;
}
/* Fisrt Tier Dropdown */
nav ul ul li {
width:170px;
float:none;
display:list-item;
position: relative;
}
/* Second, Third and more Tiers */
nav ul ul ul li {
position: relative;
top:-60px;
left:170px;
}
/* Change this in order to change the Dropdown symbol */
li > a:after { content: ' +'; }
li > a:only-child:after { content: ''; }
</style>
<!-- WITH PHP -->
<div id="container">
<nav>
<?php
//connection to the database
$dbhandle = mssql_connect('*****', '*****', '*****')
or die("Couldn't connect to Server");
//select a database to work with
$selected = mssql_select_db("*****", $dbhandle)
or die("Couldn't open database");
//declare the SQL statement that will query the database
$query = "SELECT * FROM CATEGORIES ";
//execute the SQL query and return records
$result = mssql_query($query);
//display the results
while($row = mssql_fetch_array($result))
{
// Assign by reference
$thisref = &$refs[ $row['ID'] ];
// add the the menu parent
$thisref['IDCategoriePere'] = $row['IDCategoriePere'];
$thisref['NOM'] = $row['NOM'];
$thisref['URL'] = $row['URL'];
// if there is no parent id
if ($row['IDCategoriePere'] == 0)
{
$list[ $row['ID'] ] = &$thisref;
}
else
{
$refs[ $row['IDCategoriePere'] ]['children'][ $row['ID'] ] = &$thisref;
}
}
function create_menu( $arr )
{
$html = "\n<ul>\n";
foreach ($arr as $key=>$val)
{
$html .= '<li>'.$val['NOM']."</li>\n";
if (array_key_exists('children', $val))
{
$html .= create_menu($val['children']);
}
}
$html .= "</ul>\n";
return $html;
}
echo create_menu( $list );
//close the connection
mssql_close($dbhandle);
?>
</nav>
</div>
<!-- WITHOUT PHP -->
<div id="container">
<nav>
<ul>
<li>ACCUEIL</li>
<li>PARCS
<!-- First Tier Drop Down -->
<ul>
<li>ALLEMAGNE
<!-- Second Tier Drop Down -->
<ul>
<li>BERLIN</li>
</ul>
</li>
<li>FRANCE
</li>
</ul>
</li>
<li>CONTACT</li>
</ul>
</nav>
</div>
Please give for a detailed answer all informations like your database type and also the query or all of your code. Also the column name NAME is a reserved SQL Keyword, so normally it is not a good idea but mssql is mostly not ANSI, so it is maybe not interesting :)
If you want a multidimensional menu, then you cannot only print out the table. You have first to order the data (pass all childs to parent) and then you can create your menu. For that you are using normally with recursive functions or methods like here the create_menu function.
<?php
$serverName = "serverName\instanceName";
$connectionInfo = array( "Database"=>"dbName", "UID"=>"username", "PWD"=>"password");
// connect to sql server
$conn = sqlsrv_connect( $serverName, $connectionInfo );
if( $conn === false ) {
die( print_r( sqlsrv_errors(), true));
}
// create an array to hold the references
$refs = array();
// create and array to hold the list
$list = array();
$tsql = "SELECT ID, IDPARENT, NAME, URL FROM menu_items ORDER BY NAME;"
$stmt = sqlsrv_query( $conn, $tsql);
if( $stmt === false) {
die( print_r( sqlsrv_errors(), true) );
}
while($row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC))
{
// Assign by reference
$thisref = &$refs[ $row['ID'] ];
// add the the menu parent
$thisref['IDPARENT'] = $row['IDPARENT'];
$thisref['NAME'] = $row['NAME'];
$thisref['URL'] = $row['URL'];
// if there is no parent id
if ($row['IDPARENT'] == 0)
{
$list[ $row['ID'] ] = &$thisref;
}
else
{
$refs[ $row['IDPARENT'] ]['children'][ $row['ID'] ] = &$thisref;
}
}
mssql_close($conn);
/**
*
* Create a HTML menu from an array
*
* #param array $arr
* #param string $list_type
* #return string
*
*/
function create_menu( $arr )
{
$html = "\n<ul>\n";
foreach ($arr as $key=>$val)
{
$html .= '<li>'.$val['NAME']."";
if (array_key_exists('children', $val))
{
$html .= create_menu($val['children']);
}
$html .= "</li>\n";
}
$html .= "</ul>\n";
return $html;
}
echo create_menu( $list );
?>
<?php
try
{
$db = new PDO("sqlite:/var/www/html/mobile_app.db");
print "<table border=1>";
print "<tr><td>Game ID</td><td>Game Name</td><td>Approved Version</td><td>Current Version</td><td>Status</td><td>Date Checked</td><td>Date Changed</td></tr>";
$result = $db->query('SELECT * FROM mobile_version');
foreach($result as $row)
{
print "<tr><td>".$row['gameid']."</td>";
print "<td>".$row['game_name']."</td>";
print "<td>".$row['approved_version']."</td>";
print "<td>".$row['current_version']."</td>";
print "<td>".$row['status']."</td>";
print "<td>".$row['date_checked']."</td>";
print "<td>".$row['date_changed']."</td>";
}
print "</table>";
$db = NULL;
}
catch(PDOException $e) {
echo $e->getMessage();
}
?>
My aim is to change just the cell with the data in column 'status'. Red if it equals UNAPPROVED and green if it equals APPROVED. Any help would be awesome! Data is in a single table in an sqlite3 database.
You can do it by adding the class to td and add style to that class.
In PHP
print "<td class=".$row['status'].">".$row['status']."</td>";
In CSS
td.APPROVED {
background-color: green;
color: white;
}
td.UNAPPROVED {
background-color: red;
color: white;
}
PROBLEM
I am pulling data from my database...using a WHILE loop & I want the returned data to be coupled into "scalable in size" groups.
I thought that by using a FOREACH LOOP inside of the while loop, I could accomplish this, apparently I thought wrong..ARRG!
LINK
View problem here: http://sis-cr.com/NEWSTORE/store.php?cur_str=cel
CODE
$queryCatz = mysqli_query($cxn, "SELECT DISTINCT idBrand FROM Products ");
while($list_of_xtras = mysqli_fetch_array($queryCatz)) {
$cur = 0;
$rowNum = 1;
foreach($list_of_xtras as $key => $value){
if($cur == 0){
echo '<ul style="border:2px solid purple;" class="theROW' . $rowNum . '">';
}
echo ' <li style="list-style:none;">' . $value. '</li>';
if($cur == 2)
{
echo '</ul>';
$cur = 0;
$rowNum++;
}
else
{
$cur++;
}
}
}
After 3 hours of trying to fix this, and at the risk of exposing my very obvious lack on understanding, I humbly bow before the collective minds of SO....and shout...HELP, I've coded and can't get up!
I might be off track. I think you want:
$queryCatz = mysqli_query($cxn, "SELECT DISTINCT idBrand FROM Products ");
while($list_of_xtras = mysqli_fetch_array($queryCatz)) {
$rowNum = 1;
echo '<ul style="border:2px solid purple;" class="theROW' . $rowNum . '">\r\n';
for($curr=0; $curr<2; $curr++){
echo '\t<li style="list-style:none;">' . $list_of_xtras[$curr] . '</li>\r\n';
}
echo '</ul>';
$rowNum++;
}
Seems like you're just looking for 2 results, so I don't know if FOREACH is best here. Maybe just a FOR loop.
Edit, after I saw your comment. Sounds like you want to wrap 10 results in a UL. To do that it would be something like:
$rowNum = 1;
echo '<ul style="border:2px solid purple;" class="theROW' . $rowNum . '">\r\n';
$queryCatz = mysqli_query($cxn, "SELECT DISTINCT idBrand FROM Products ");
while($list_of_xtras = mysqli_fetch_array($queryCatz)) {
if($rowNum % 10 == 0){
echo '</ul><ul style="border:2px solid purple;" class="theROW' . $rowNum . '">\r\n';
}
echo '\t<li style="list-style:none;">' . $list_of_xtras[0] . '</li>\r\n';
$rowNum++;
}
echo "</ul>\r\n";
I'm trying to display the results of my query into a three columns for each row returned.
The setup is 3 divs all floating left inside one large wrapper. Easy enough, yeah?
#wrapper {width: 650px;}
#left {width: 200px; float: left;}
#middle {width: 200px; float: left;}
#right {width: 200px; float: left;}
Results displayed like:
LEFT | MIDDLE | RIGHT
LEFT | MIDDLE | RIGHT
LEFT | MIDDLE | RIGHT
LEFT | MIDDLE | RIGHT
Only now, I have this mysql associative query I want to display the results of in each column.
To be simple, I have Column A that needs to be in the left div.
Columns B through Y in middle div.
Column Z in right div.
Here's my query:
while ($row = mysql_fetch_assoc($result)) {
foreach ($row as $col => $val) {
if ($col == "Column A") {
//How to get this in left div?
}
if ($col != "Column A" && $col != "Column Z") {
//How to get this in middle div?
}
if ($col == "Column Z") {
//How to get this in right div?
}
}
}
I'm really not sure where to even start.
Always try seperating the view (HTML) from the actual code. You might want to read a bit about the MVC arhitecture. It might seem hard to understand at first, but trust me - it's worth learning how to use it.
<?php
$column = array(
'A' => array(),
'B' => array(),
'Z' => array()
);
while ($row = mysql_fetch_assoc($result)) {
foreach ($row as $col => $val) {
if ($col == "Column A") {
$column['A'][] = $row;
}
if ($col != "Column A" && $col != "Column Z") {
$column['B'][] = $row;
}
if ($col == "Column Z") {
$column['Z'][] = $row;
}
}
}
?>
<style type="text/css">
div {width:200px;float:left}
</style>
<!-- 1st column -->
<div><?php foreach ($column['A'] AS $row) { ?>...<?php } ?></div>
<!-- 2nd column -->
<div>..</div>
<!-- 3rd column -->
<div>..</div>
Cheers!
You have quite many options here. But as i would do this is to make 3 or 1 array(s) then in my template iterate them in html you got there.
Something like:
$col_left = array();
$col_mid = array();
$col_right = array();
while ($row = mysql_fetch_assoc($result)) {
foreach ($row as $col => $val) {
if ($col == "Column A") {
$col_left[] = $val;
}
if ($col != "Column A" && $col != "Column Z") {
$col_mid[] = $val;
}
if ($col == "Column Z") {
$col_right[] = $val;
}
}
}
Then just in html loop those and your done. There's many more options to go, but this would be from the top of my head.
Then in html:
<div id="left">
<?php
foreach($col_left as $result){
echo $result.'<br/>';
}
?>
</div>
Something like that, ofc you can add checks for empty etc. there.
I would do it slightly autonomously, taking your current identifiers into account. I've just dumped all of the content into an array, and joined it with line breaks at the bottom. My example might be a little too general, but it certainly gets the job done, and tweaks are completely possible :)
<?
$columns = array(
'left' => array(),
'middle' => array(),
'right' => array()
);
while ($row = mysql_fetch_assoc($result)) {
foreach ($row as $col => $val) {
if ($col == "Column A") {
$columns['left'][] = $val;
} elseif ($col == "Column Z") {
$columns['right'][] = $val;
} else {
$columns['middle'][] = $val;
}
}
}
?>
<div id="wrapper">
<? foreach($columns as $column => $value_array) { ?>
<div id="<?= $column ?>">
<?= join("<br />", $value_array) ?>
</div>
<? } ?>
</div>
I did this exact thing, but I put the result into a table. I used the modulus function with a helper counter variable.
if (($artisttablecount % 3) == 0){
echo "</tr><tr>";
}
I'm not sure how to do it with the divs, but the mod function may help. Full code below.
//QUERY
$artisttable = mysql_query("SELECT DISTINCT * FROM records GROUP BY artist ORDER BY artist ASC");
//HELPER VAR FOR PRINTING IN 3 ROWS
$artisttablecount=0;
//LOOP THROUGH RESULTS
while($row = mysql_fetch_array($artisttable)){
//MAKES TABLE 3 COLUMNS
if (($artisttablecount % 3) == 0){
echo "</tr><tr>";
}
$current=$row['Artist'];
$artcount = mysql_num_rows(mysql_query("SELECT * FROM records WHERE artist = '$current'"));
$spaceless = str_replace(" ", "%20", $row['Artist']);
echo "<td><a href=inventory.php?dec=0&format=0&art='" . $spaceless . "'>" . $row['Artist'] . "</a> ($artcount)</td>";
$artisttablecount++;
}
?>
we've codeded this PHP-Script:
function price($id)
{
$ergebnis = mysql_query("SELECT * FROM preiszuordnungen where id=$id");
while($row = mysql_fetch_object($ergebnis))
{
//Wenn grundpreis dann färben
if($row->typ=="grundpreis")
{
$gp="style=\"background-color: #FF22FF; color: #FFFFFF; padding: 5px;\"";
}
else
{
}
//preisfomat umändern
$preis="+ ".number_format($row->preis,2)." EUR";
$GLOBALS["$id"] = $row->preis;
//zeile ausgeben
echo"<div $gp class=\"rechnung_bezeichnung\">$row->bezeichnung</div><div $gp class=\"rechnung_preis\">$preis</div><p style=\"clear: both;\">";
}
}
our Problem is that we cant use the variable out of this function. In our Script we need the function like this:
$preis=price(101);
$preis=price(909);
...
Now we want to sum the output of the function. variable $row->price
We're happy if someone could help us.
just add
return $row->preis;
to the end of the function.