Place 2 PHP generated HTML tables side by side - php

I have generated 2 HTML tables with PHP. The first table is always ontop of the 2nd table. I can't seem to get them to be side by side. the 2nd one is always under the first table.
I've also tried adding an html style of float left as well as inline in the html for the tables and it still doesn't come out side by side. Any help would be greatly appreciated!
//add table for bids and asks
function build_table($bidarray){
// start table
$html = '<table style="display: inline-block;">';
// header row
$html .= '<tr>';
foreach($bidarray[0] as $key=>$value){
$html .= '<th>' . htmlspecialchars($key) . '</th>';
}
$html .= '</tr>';
// data rows
foreach( $bidarray as $key=>$value){
$html .= '<tr>';
foreach($value as $key2=>$value2){
$html .= '<td>' . htmlspecialchars($value2) . '</td>';
}
$html .= '</tr>';
}
// finish table and return it
$html .= '</table>';
return $html;
}
$bidarray = array(
array('Company'=>'cardsltd', 'Min Qty'=>'5', 'Max Qty'=>'10', '$/box'=>'5.00'),
);
$askarray = array(
array('Company'=>'comp', 'Min Qty'=>'4', 'Max Qty'=>'9', '$/box'=>'4.00'),
);
echo build_table($bidarray) . build_table($askarray) ;

You need to use <div>s.
Take a <div>
Put two <div>s in it containing a table each.
<div>
<div style="float:left; width: 49%">
<table>
...
</table>
</div>
<div style="float:left; width: 49%">
<table>
...
</table>
</div>
</div>
This will put your tables side by side.
Also, we can change/adjust/manage widths.
49% is just for demo purposes.

PHP Code:
//define class for table
$html = '<div class="half-width"><table> ...';
CSS code:
.half-width
{
position: relative;
width: 100%;
padding-right: 15px; /*optional*/
padding-left: 15px; /*optional*/
-ms-flex: 0 0 50%;
flex: 0 0 50%;
max-width: 50%;
}
Also you can add float:right;width:50% for defined class (here class name is half-width)

Related

Display PHP While Loop Horizontally & Vertically Using Flexbox

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>

PHP creating hidden hyperlinks on pages

I have a custom component being used on a Joomla 3.9 site.
The below code creates hyperlinks with proper anchors ( for 2 pages ) from a database and works OK except that it's also creating irrelevant hidden links.
You can only see the href links if you view the source of the page.
i.e.
What can I try here to make sure the code doesn't create these hidden links?
else:
JFactory::getDocument()->addStyleDeclaration('#media (max-width:540px){table.all-regions tr td{width: 100% !important;display: inline-block !important;text-align: center !important;}}');
$regionsList = '';
$regionsList .= '<table class="all-regions">';
$regions = $this->get('regions');
for($i = 0;$i <= count($regions); $i+=3):
$regionsList .= '<tr>';
$regionsList .= '<td style="width: 1%;padding: 9px; line-height: 24px;">'.$regions[$i]->region_name.'</td>';
$regionsList .= '<td style="width: 1%;padding: 9px; line-height: 24px;">'.$regions[$i+1]->region_name.'</td>';
$regionsList .= '<td style="width: 1%;padding: 9px; line-height: 24px;">'.$regions[$i+2]->region_name.'</td>';
$regionsList .= '</tr>';
endfor;
$regionsList .= '</table>';
$text = JString::str_ireplace('{%regions_list%}', $regionsList, $this->article->text);
$this->setBreadcrumbs(
array(
'country'
)
);
endif;
echo $text;
Details that may or may not help:
One page shows 3 hidden links and the other 2 hidden links. The hidden links point to the same page I'm currently on. The page with 3 hidden links is showing 3 even columns of links while the one with 2 hidden links has one row with 5 links and the other 2 rows with 4 links
There are better ways to do this whole thing, but without rewriting the code, you need to see if those elements are set and aren't empty or else you will get empty anchors:
if(!empty($regions[$i]->region_name)) {
$regionsList .= '<td style="width: 1%;padding: 9px; line-height: 24px;">'.$regions[$i]->region_name.'</td>';
}
if(!empty($regions[$i+1]->region_name)) {
$regionsList .= '<td style="width: 1%;padding: 9px; line-height: 24px;">'.$regions[$i+1]->region_name.'</td>';
}
if(!empty($regions[$i+2]->region_name)) {
$regionsList .= '<td style="width: 1%;padding: 9px; line-height: 24px;">'.$regions[$i+2]->region_name.'</td>';
}
As #AbraCadaver hinted, your code could use some DRYing out and some polishing up.
Differing from Abra's suggested resolution, it is important to consistently output the same number of cells in each row of the html table for valid html markup. If there is no region data at a particular index, just print an empty cell.
Rather than repeating the same cell markup text and inserting manually incremented indexes, use a cell template string and a for() loop.
I also recommend moving all your styling to the css declaration and referencing .all-regions td.
Suggested rewrite (not tested)
$html = '<table class="all-regions">';
$cellTemplate = '<td style="width: 1%; padding: 9px; line-height: 24px;">%s</td>';
$regions = $this->get('regions');
foreach (array_chunk($regions, 3) as $chunk) {
$html .= '<tr>';
for ($i = 0; $i < 3; ++$i) {
if (!isset($chunk[$i])) {
$html .= '<td></td>';
} else {
$html .= sprintf(
$cellTemplate,
JUri::root(),
$country
strtolower(str_replace(' ', '-', $chunk[$i]->region_name)),
$chunk[$i]->region_name
);
}
}
$html .= '</tr>';
}
$html .= '</table>';
$text = JString::str_ireplace('{%regions_list%}', $html, $this->article->text);
$this->setBreadcrumbs(['country']);

increment id for dynamically generated div

I want to dynamically generate video player container divs that will have a unique id. The elements are generated through Visual Composer in WordPress and the final html should look something like this:
<div style="width: 100%; display: inline-block; position: relative;">
<div style="margin-top: '. $custom_margin .'"></div>
<div id="player_1" style="position:absolute;top:0;left:0;right:0;bottom:0"></div>
</div>
<div style="width: 100%; display: inline-block; position: relative;">
<div style="margin-top: '. $custom_margin .'"></div>
<div id="player_2" style="position:absolute;top:0;left:0;right:0;bottom:0"></div>
</div>
<div style="width: 100%; display: inline-block; position: relative;">
<div style="margin-top: '. $custom_margin .'"></div>
<div id="player_3" style="position:absolute;top:0;left:0;right:0;bottom:0"></div>
</div>
The user should be able to add as many div elements with unique ids as he likes. This is how my php looks right now:
EDIT: The function is called for every single video container
public function vc_custvideo_html($atts){
extract(
shortcode_atts(
array(
'custom_width' => '',
'custom_height' => '',
),
$atts
)
);
$percent = $custom_height / $custom_width;
$custom_margin = number_format( $percent * 100, 2 ) . '%';
$html = '';
$html.= '<div style="width: 100%; display: inline-block; position: relative;">';
$html.= '<div style="margin-top: '. $custom_margin .'"></div>';
$html.= '<div id="player_" style="position:absolute;top:0;left:0;right:0;bottom:0"></div>';
$html.= '</div>';
return $html;
}
I can understand that i have to use a foreach loop to generate the unique ids but i'm really new at php and I need help.
Something like that
$i = 0;
foreach ($playerInstance as $k => $currPlayer {
vc_custvideo_html($currPlayer, $i)
$i++;
}
public function vc_custvideo_html($atts, $index){
extract(
shortcode_atts(
array(
'custom_width' => '',
'custom_height' => '',
),
$atts
)
);
$percent = $custom_height / $custom_width;
$custom_margin = number_format( $percent * 100, 2 ) . '%';
$html = '';
$html.= '<div style="width: 100%; display: inline-block; position: relative;">';
$html.= '<div style="margin-top: '. $custom_margin .'"></div>';
$html.= '<div id="player_'.$index.'" style="position:absolute;top:0;left:0;right:0;bottom:0"></div>';
$html.= '</div>';
return $html;
}
Or with a for loop maybe
Assuming the function is called once per Video, use an outer foreach and a counter:
$counter=1;
foreach($arrayOfVideos as $video){
$this->vc_custvideo_html($video, $counter);
$counter++;
}
And inside your function, just use the $counter variable:
// [...]
$html.= '<div style="margin-top: '. $custom_margin .'"></div>';
$html.= '<div id="player_"' . $counter . ' style="position:absolute;top:0;left:0;right:0;bottom:0"></div>';
$html.= '</div>';

making dropdown in the very center of the page

I wanna make the dropdown list to appear in the center of the page. How do I do that?
Code:
echo '<div class="dropCenter" align="center">';
echo '<label><SELECT name="projectDropdown" id="projectDropdown" class="projectSelect">'.'<br>';
echo '<OPTION VALUE=" ">'."".'</OPTION>';
while($row = oci_fetch_array($compCuttingResult,OCI_ASSOC)) {
$projectName = $row ['PROJECT_NAME'];
echo "<OPTION VALUE='$projectName'>$projectName</OPTION>";
}
echo '</SELECT></label><br />';
First, remove all BR and LABEL tags from the HTML markup.
Next append a closind DIV tag at the end of the markup.
PHP Code
$html = null;
$html .= "<div class=\"dropCenter\">";
$html .= "<select id=\"projectDropDown\" name=\"projectDropDown\">";
$html .= "<option value=\"\"></option>";
while($row = oci_fetch_array($compCuttingResult,OCI_ASSOC)){
$projectName = $row['PROJECT_NAME'];
$html = sprintf("<option value=\"%s\">%s</option>", $projectName, $projectName);
}
$html .= "</select>";
$html .= "</div>";
print $html;
CSS Code
<style type="text/css">
select.projectSelect{ width:280px; height:24px; margin:10px; }
select.projectSelect option{line-height:24px;}
div.dropCenter{ display:inline-block; position:absolute; width:300px; height:34px; margin-left:-150px; margin-top:-17px; background-color:#e0e0e0; border:1px solid #c0c0c0; }
</style>
use the style to the first div
margin: 0px auto;
This should put the div in center
You can use this code
margin:auto;
<div class="dropCenter" style= 'margin:auto;'>
also you will have to check what are the divs you have above this. If there is no effect with above divs this would work fine.
External Inherited Style Sheets will also have an effect.

css div not being recognized

I have the following css code:
#Layer3
{
position:absolute;
width: 89%;
height: 40%;
left: 10%;
top: 56%;
background-color: #f1ffff;
}
#Layer3 h1
{
font-size: medium;
color: #000033;
text-decoration: none;
text-align: center;
}
.tableheader {
border-width:10px; border-style:solid;
}
.tablecontent {
height: 95%;
overflow:auto;
}
However, when I use this PHP to generate the html
echo '<div id="tableheader" class="tableheader">';
echo "<h1>{$query} Auctions</h1>" . "\n";
echo "</div>";
echo '<div id="tablecontent" class="tablecontent">';
echo "<table border='0' width='100%'><tr>" . "\n";
echo "<td width='15%'>Seller ID</td>" . "\n";
echo "<td width='10%'>Start Date</td>" . "\n";
echo "<td width='75%'>Description</td>" . "\n";
echo "</tr>\n";
// printing table rows
foreach ($rows as $row)
{
$pk = $row['ARTICLE_NO'];
echo '<tr>' . "\n";
table contens generated in here
echo '</tr>' . "\n";
}
echo "</table>";
}
echo "</div>";
which generates this html:
<div id="tableheader" class="tableheader">
<h1>hardy Auctions</h1>
</div>
<div id="tablecontent" class="tablecontent">
<table border='0' width='100%'>
<tr>
<td width='15%'>Seller ID</td>
<td width='10%'>Start Date</td>
<td width='75%'>Description</td>
the rest of table stuff
</div>
The stylesheet is correctly referenced so I am unsure what is causing the error. But there is no border around tableheader at all. Both of these layers are in Layer3 which no longer displays properly on the page.
#tableheader {
border: 10px solid #000;
}
Try giving it a color.
EDIT: since its id is tableheader, try changing the style selector to be an id. You could also try using !important to see if anything is overriding your class selector.
Specificity values:
inline: 1000; id: 100, class: 10, element: 1
!important trumps all other non-important declarations.
Start by browsing the HTML DOM in the rendered page either using Firebug in Firefox or using the IE Developer Toolbar in IE.
That way, you can see what styles are actually associated with the element in the rendered page. It's a lot easier to debug the issue from there.
One possibility is that there's a syntax error somewhere in the CSS file causing the styles not to be applied correctly.
I've just had a quick look at this and it seams fine, I've also created a local copy of these and the styles work out okay as well, I get a nice thick black border around the h1 text.
So from what your explaining something is either overwriting the styles, or the styles aren't being applied to the page.

Categories