How to represent a binary tree with tables (html)? - php

Here is a brain-teaser for the brave. I've been at it for days and just can't come with the solution.
I wanted to come out with something like this:
Using html, CSS and PHP only.
I got near, but not quite what I expected. Here is the code in PHP and here is the output.
<table border="0">
<thead>
<tr>
<th>Cientoveintiochavos</th>
<th>Seseintaicuatravos</th>
<th>Treintaidosavos</th>
<th>Dieciseisavos</th>
<th>Octavos</th>
<th>Cuartos</th>
<th>Semifinales</th>
<th>Final</th>
</tr>
</thead>
<tbody>
<?php for($i=0;$i<256;$i++): ?>
<tr>
<?php for($n=0,$c=2;$n<8;$n++,$c*=2): ?>
<?php
/*
if(false){//$i == 0) {
$rwspn = $c/2+1;
$iter = 0;
} else {
$rwspn = $c;
$iter = $c;//-$c/2+1;
}
*/
$class = ($i%($c*2))?'par':'impar winner';
if($i%$c==0):?>
<td rowspan="<?=$c;?>" class="<?=$class;?>"><span><?php echo genRandomString();?></span></td>
<?php endif; ?>
<?php endfor; ?>
</tr>
<?php endfor; ?>
</tbody>
</table>
If someone knows how to represent a binary tree or a dendrogram or comes up with a smarter code please let me know!

I've done something like this, using divs kinda like #HugoDelsing. The way I handled the lines was to divide each pair into 4 vertically-stacked divs:
The first player (border-bottom)
A spacer between 1st and 2nd players (border-right)
The second player (border-bottom and border-right)
A spacer before the next pair (no borders)
Each of these gets 1/4 the height of the pair*, and the total height of a pair gets doubled as you move to the right. If you don't have a power of two, fill slots with placeholders to push everything down the right amount.
*The bottom borders will throw the heights off by 1, so take that into account when styling your rows.
Other Notes
The spacer divs may not be necessary, but for me they easily handled the spacing and getting the different columns to line up correctly.
I used inline styles filled-in by PHP for the heights, so I didn't have an arbitrary depth limit or calculations hard-coded into CSS.
Here's an example.
EDIT
OK, here is teh codez:
<style type="text/css">
.round{
float:left;
width:200px;
}
.firstTeam, .secondTeam{
border-bottom:1px solid #ccc;
position:relative;
}
.firstSpacer, .secondTeam{
border-right:1px solid #ccc;
}
.team{
position:absolute;
bottom: 4px;
left: 8px;
}
</style>
<div class="round">
<div class="matchup">
<div class="firstTeam" style="height:29px;"><div class="team">Team One</div></div>
<div class="firstSpacer" style="height:30px;"> </div>
<div class="secondTeam" style="height:29px;"><div class="team">Team Two</div></div>
<div class="secondSpacer" style="height:30px;"> </div>
</div>
<div class="matchup">
<div class="firstTeam" style="height:29px;"><div class="team">Team Three</div></div>
<div class="firstSpacer" style="height:30px;"> </div>
<div class="secondTeam" style="height:29px;"><div class="team">Team Four</div></div>
<div class="secondSpacer" style="height:30px;"> </div>
</div>
<div class="matchup">
<div class="firstTeam" style="height:29px;"><div class="team">Team Five</div></div>
<div class="firstSpacer" style="height:30px;"> </div>
<div class="secondTeam" style="height:29px;"><div class="team">Team Six</div></div>
<div class="secondSpacer" style="height:30px;"> </div>
</div>
<div class="matchup">
<div class="firstTeam" style="height:29px;"><div class="team">Team Seven</div></div>
<div class="firstSpacer" style="height:30px;"> </div>
<div class="secondTeam" style="height:29px;"><div class="team">Team Eight</div></div>
<div class="secondSpacer" style="height:30px;"> </div>
</div>
</div>
<div class="round">
<div class="matchup">
<div class="firstTeam" style="height:59px;"><div class="team">Team One</div></div>
<div class="firstSpacer" style="height:60px;"> </div>
<div class="secondTeam" style="height:59px;"><div class="team">Team Three</div></div>
<div class="secondSpacer" style="height:60px;"> </div>
</div>
<div class="matchup">
<div class="firstTeam" style="height:59px;"><div class="team">Team Five</div></div>
<div class="firstSpacer" style="height:60px;"> </div>
<div class="secondTeam" style="height:59px;"><div class="team">Team Eight</div></div>
<div class="secondSpacer" style="height:60px;"> </div>
</div>
</div>
<div class="round">
<div class="matchup">
<div class="firstTeam" style="height:119px;"> </div>
<div class="firstSpacer" style="height:120px;"> </div>
<div class="secondTeam" style="height:119px;"> </div>
<div class="secondSpacer" style="height:120px;"> </div>
</div>
</div>
<div class="round">
<div class="matchup">
<div class="firstTeam" style="height:239px;"> </div>
</div>
</div>

I wouldnt use a table but divs.
create a column container div with relative/absolute position with a fixed width (eg: 200px) for each col.
Each column container has inner divs with a height and lineheight of double the previous column container
create a long black vertical line image (length atleast the half the size of the largest height of inner divs in any column. Start the line with a horizontal line of 200px wide to the left (so rotate an L with 180degrees). Leave about half the text height of free space above the horizontal line in the image, so the line will be under the text.
set this image as background to the inner div of each column container and position it at left center; repeat = none;
Some sample code (without images)
<style type="text/css">
div.col { position:absolute;border:1px solid #f00;width:200px;top:0px; }
div.col1 { left:0px; }
div.col1 div { height:20px; line-height:20px; }
div.col2 { left:200px; }
div.col2 div { height:40px; line-height:40px; }
div.col3 { left:400px; }
div.col3 div { height:80px; line-height:80px; }
div.col4 { left:600px; }
div.col4 div { height:160px; line-height:160px; }
div.col5 { left:800px; }
div.col5 div { height:320px; line-height:320px; }
</style>
<div class='col1 col'>
<div>player1</div>
<div>player2</div>
<div>player3</div>
<div>player4</div>
<div>player5</div>
<div>player6</div>
<div>player7</div>
<div>player8</div>
<div>player9</div>
<div>player10</div>
<div>player11</div>
<div>player12</div>
<div>player13</div>
<div>player14</div>
<div>player15</div>
<div>player16</div>
</div>
<div class='col2 col'>
<div>player1</div>
<div>player3</div>
<div>player5</div>
<div>player7</div>
<div>player9</div>
<div>player11</div>
<div>player13</div>
<div>player15</div>
</div>
<div class='col3 col'>
<div>player1</div>
<div>player5</div>
<div>player9</div>
<div>player13</div>
</div>
<div class='col4 col'>
<div>player1</div>
<div>player9</div>
</div>
<div class='col5 col'>
<div>player1</div>
</div>

Looks like you're almost there. Nice work! I think the center alignment you want is in CSS
td {
vertical-align: middle;
}
I don't think you can get the lines to work using borders. You might try a background image for them instead.

Related

I cannot make php to replace a <p> text by my $_SESSION['userlogged'] value

I have tried
<?php session_start(); ?>
.
.
.
<p><?php $_SESSION['loggeduser']; ?>;</p>
I have also tried this JS after the
<script type="text/javascript">
let vuser = '<%= session.getAttribute("loggeduser") %>';
document.getElementById("userid").value= "Not set";
</script>
If someone can tell me what am I doing wrong please!
This is my full body code:
<body>
<section id="MyHeader" style="border:solid red thin;">
<div id="TheHead" style="width:80%; display:inline-block;">
<div id="HeadImg" style="width: 100px; height:100px;">
<a href="main.html">
<img src="../images/icon-redcross.png" alt="AGR">
</a>
</div>
<div id="HeadLegend">
<div id="AGRbrand"><h1>AGR</h1></div>
<div id="quote">
<span style="font-size: small;"><i>Properties catalogue</i></span>
</div>
</div>
</div>
<div id="user" style="display:inline-block; width:12%; height:25px;">
<p id="userid" style="width:100%;"><?php $_SESSION['loggeduser'];?></p>
<script type="text/javascript">
let vuser = '<%= session.getAttribute("loggeduser") %>';
document.getElementById("userid").value= "Not set";
</script>
</div>
<div style="width:100%; display:block;">
<div id="MenuButtons" style="width:80%; display:flex; justify-content:right;">
<div id="newbtn" class="TopButton">New</div>
<div id="exitbtn" class="TopButton">Exit</div>
</div>
</div>
<div class="Subtitle">
<div style="display: flex; justify-content: left; width:100%;">
<h4>Properties</h4>
</div>
</div>
</section>
<div id="Estates" class="pagebreak"></div>
<section id="MyPage">
<div id="Home"></div>
<div class="page" style="width:100%; background-color: white; ">
<div style="display: flex; justify-content:center; width:100%;">
<div class="ServiceBox">
<div class="div-img">
<img src="../../images/house-B.png" alt="AGR">
</div>
<div class="div-info">
</div>
<div class="div-desc">
</div>
<div class="div-btns">
<a class="SideButton" href="Edit.html">Edit</a>
<a class="SideButton" href="Delete.html">Delete</a>
<a class="SideButton" href="pdf-file.html">Pdf</a>
</div>
</div>
</div>
</div>
</section>
</body>
Notes: $_SESSION["loggeduser"] is populated before this page,at the login page; I verified the variable was properly set by a log file I have, and I was able to see the value printed in the log, after the user logs in and before this page is loaded.
Not sure if it is the php version or the goDaddy servers, but in my case the problem was, I was using html extension for the files, I changed them to .php extension and the php code started to work.

How can i set a height to div according to step by step div

When i am listing products i want them to have fixed height according to its step by step div's max height. For example below there is a screenshot from amazon that has what i want. All 4 elements' height are same.
and i'm using the code below to list products
<div class="shop-product-wrap">
<div class="row row-8">
<?php foreach ($products as $product): ?>
<div class="product-col col-lg-3 col-md-4 col-sm-6">
<!-- Single Product Start -->
<div class="single-product-wrap mt-10">
<div class="product-image">
<a href="<?= $product['url'] ?>"><img class="lazy"
src="<?= public_url('images/other/thumbnail.jpg') ?>"
data-src="<?= image_url($product['image_small']) ?>"
alt=""></a>
</div>
<div class="product-button">
<a onclick="wishlist.add(<?= $product['product_id'] ?>)"
class="add-to-wishlist">
<i class="icon-heart"></i></a>
</div>
<div class="product-content">
<h6 class="product-name"><?= $product['product_name'] ?>
</h6>
<div class="starts-icon-box">
<i class="stars-icon"></i>
</div>
<div class="price-box">
<div class="single-product-discount-and-price">
<?php if ($product['product_discount_price']): ?>
<span class="onsale"><?= (100 - (round(($product['product_price'] * 100) / $product['product_discount_price']))) . '%' ?></span>
<?php endif; ?>
<div class="old-and-new-price">
<span class="old-price"><?= $product['product_discount_price'] ?> TL</span>
<span class="new-price"><?= number_format($product['product_price'], 2) ?> TL</span>
</div>
</div>
</div>
<div class="product-button-action">
<a onclick="cart.add(<?= $product['product_id'] ?>,1)"
class="add-to-cart">Add to cart</a>
</div>
</div>
</div>
<!-- Single Product End -->
</div>
<?php endforeach; ?>
</div>
</div>
and when i listing products they all take the height they can according to their content such as its name and so that they don't have the same height. How can i make them have same height step-by-step.
Sorry for my bad english.
You have to use div tag with same class name for every product item and add a parent div like this...
<div class="parent">
<div class="child">Item 1</div>
<div class="child">Item 2</div>
<div class="child">Item 3</div>
<div class="child">Item 4</div>
</div>
Then use flex box for parent and child element...
.parent{
display: flex;
flex-wrap: wrap;
align-content: stretch;
height: 600px; /*or something*/
}
.child{
background-color: #f1f1f1;
width: 100px;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
}
Please visit those links for more information about flex box...
https://www.w3schools.com/css/css3_flexbox.asp
https://www.w3schools.com/css/tryit.asp?filename=trycss3_flexbox_align-content_stretch

How do I align different size images

I'm trying to write a page with different size images but can't figure out how to align them properly.
This is how the web-page looks:
https://i.imgur.com/Li17CMl.jpg
I have added bootstrap img-fluid for responsiveness. If i set fixed height images wont scale down properly on smaller screens.
I cleaned some non related data with '...' just to make code look cleaner.
My current code:
foreach(row...) : ?>
<div class="col-md-6" style="padding...">
<div class="card">
<div class="card-body" style="padding...">
<a href="">
<div>
<img class="img-fluid" style="width: 100%;" src="row->image..." alt="">
</div>
<div>
<h5> title </h1>
<h6> date </h6>
</div>
</a>
</div> <!-- /card body -->
</div> <!-- /card -->
</div> <!-- /col-6 -->
<?php endforeach; ?>
I'm hoping that images will scale down on smaller screens.
TL:DR object-fit: cover; will do the trick.
Solution 1 - FIXED HEIGHT
with this solution you have to set value for the height of the images: the photos won't stretch but the ratio will be different for each screen
.img-fluid {
height: 200px; /* insert here your desired height*/
object-fit:cover;
}
Solution 2 - REAL FLUID
this solution is a little trickier but the photos will have always the ratio you will choose.
.card-body a div:first-child {
position: relative;
width: 100%;
height: 0;
padding-top: 60%; /*insert value for the desired ratio. ie: 60% -> 10/6 image*/
}
.img-fluid {
position: absolute;
top:0;
left: 0;
right: 0;
bottom: 0;
object-fit: cover;
}
Please use below code:
.img-card img{width:100%;height:300px;object-fit:cover;}
.img-card{margin-bottom:15px;margin-top:15px;}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<div class="container">
<div class="row">
<div class="col-6">
<div class="img-card">
<img src="https://i.ytimg.com/vi/VDNd4KjELkU/maxresdefault.jpg" />
</div>
</div>
<div class="col-6">
<div class="img-card">
<img src="https://media-cdn.tripadvisor.com/media/photo-s/0f/5e/fc/f5/inkaya-cinari.jpg" />
</div>
</div>
<div class="col-6">
<div class="img-card">
<img src="https://thumbs.dreamstime.com/z/scenery-5680647.jpg" />
</div>
</div>
<div class="col-6">
<div class="img-card">
<img src="https://cdn.pixabay.com/photo/2015/11/11/03/47/evening-1038148__340.jpg" />
</div>
</div>
</div>
</div>

PHP conditions and loops are breaking html formatting

I'm trying to convert my html template into a php script that simply takes returned database values and uses comparisons on certain values to place the correct content in the placeholders.
The data is being returned properly and is showing on the page but my html formatting is breaking.
This should output a very simple format of a container row, 2 half-width columns each of which with its own internal div like this:
<div class="row middle">
<div class="col-lg-6 leftFifty">
<div class="leftContent" style="background-color: white; height: 100%; ">
Content
</div>
</div>
<div class="col-lg-6 rightFifty">
<div class="rightContent" style="background-color: white; height: 100%; ">
Content
</div>
</div>
</div>
But when I add in the PHP conditions it breaks the format completely including anything included after this section
<div class="row middle">
<?php foreach($panelResult as $PR): ?>
<div class="col-lg-6 leftFifty">
<?php if($PR['panel_type_id'] == 2){ ?>
<div class="leftContent" style="background-color: white; height: 100%; ">
<?php echo $PR['content']?>
</div>
</div>
<div class="col-lg-6 rightFifty">
<?php } elseif($PR['panel_type_id'] == 3){?>
<div class="rightContent" style="background-color: white; height: 100%; ">
<?php echo $PR['content'] ?>
</div>
<?php } ?>
</div>
<?php endforeach; ?>
</div>
<!--This div is not showing-->
<div class="row bottom">
<div class="col-lg-12">
<div class="marquee"><h2>This is a test</h2></div>
</div>
</div>
For this page type, row-middle should exist, and both the leftFifty/leftContent class as well as the rightFifty and rightContent class should all exist.
The only reason I'm using the If statement is to make sure that the panel_type_id ==2 content fills the left div, and same for the right div if it equals 3.
How can I restructure this to retain the html formatting?

Align divs with different heights using css

Hi I want to align two rows of div under each other no matter the height of the divs in the row above. I'm using display:inline-block; so that the divs will inline next to each other.
Here is what I'm getting
Photo 1
Notice that div #2 is slightly shorter than div one
and here is what i want to accomplish
enter image description here
Notice that in the second picture no matter the height of the div above it the second row still aligns as it should.
here is my css
.post-set{
max-width:445px;
margin:0px 1px 15px 0px;
padding:0;
display:inline-block;
border-bottom:1px solid #e1e1e1;
vertical-align:top;
}
i'm using this in my wordpress theme. Here is the html along with the php.
<div class="medium-8 column post-set" style="padding:0;">
<a href="<?php the_permalink(); ?>" title="<?php printf(__( 'Read %s', 'wpbx' ), wp_specialchars(get_the_title(), 1)) ?>">
<?php the_post_thumbnail('fifth-post'); ?>
</a>
<div class="row column" style="padding:0">
<h1 class="fifth-post-title"><?php the_title(); ?><h1>
<div class="fifth-ex">
<?php echo excerpt(18); ?>
</div>
<span class="fifth-by-line"> BY: <?php the_author_posts_link(); ?></span>
</div>
</div>
You can visit GetVersed.us to see a live example. It's the section under the "Get Your Voice Heard Banner"
is there a way to accomplish this without suggesting masonry?
I don't see HTML you are using. But here is solution with my html code and yours CSS class. Notice that in CSS I set width instead of max-width.
Different height of div blocks I set using inline style. It can be set in class or auto.
.post-set{
width:60px;
margin:0px 1px 15px 0px;
padding:0;
display:inline-block;
border-bottom:1px solid #e1e1e1;
vertical-align:top;
background-color:black;
}
<div>
<div class="post-set" style="height:20px;">
</div>
<div class="post-set" style="height:35px;">
</div>
</div>
<div>
<div class="post-set" style="height:15px;">
</div>
<div class="post-set" style="height:30px;">
</div>
</div>
You can create a simple table and place your divs as follows:
<table>
<tbody>
<tr>
<td>
<div 1...>
</td>
<td>
<div 2...>
</td>
</tr>
<tr>
<td>
<div 3...>
</td>
<td>
<div 4...>
</td>
</tr>
</tbody>
</table>
This will make sure that the alignment is as you wish. You can then add some styles if you wish.
You should look into flexbox.
HTML:
<div class="item_container">
<div class="item">
</div>
<div class="item">
</div>
</div>
<div class="item_container">
<div class="item">
</div>
<div class="item">
</div>
</div>
CSS:
.item_container {
display:flex; align-items:stretch;
}
.item { float:left; }

Categories