How to display data in two columns with PHP "Labels print"? - php

What should I do so that I can not do the following? I want to print out the results as an address label.
1 2
3 4
5 6
7 8
... ...
Sample view

Here's a solution echoing html table tags and variables, you could also use css. Some will say css is better, but this sort of tabular arrnagement of data is exactly the reason table tags were designed. $ variable fields will automatically be identified as variables and parsed to the undsrlying inofrmation in memory, so you do not need to concatenate the fields and strings, but it makes the code more precise and readable. You could just embed the variables in the string if you wish.
<?php
$c1r1="col1row1";
$c2r1="col2row1";
$c1r2="col1row2";
$c2r2="col2row2";
echo "
<table border \"=2\">
<tr><td>". $c1r1."</td><td>".$c2r1."</td></tr>
<tr> <td>".$c1r2."</td> <td>".$c2r2."</td></tr>
</table>";
?>
Reference to official w3 docs on tables

Related

Order of execution between text and a class-generated table

I have a problem with a HTML table generated with a PHP class and saved into a variable.
This variable also contains text, saved before the table, but the table is displayed before the text.
Here's what I do :
Put a text in a variable :
$RET='Awesome introduction';
Generate a table with a class of my own (it's still a simple table) :
$TAB=new class_table();
$TAB::add_th('Column'); // saves 'Column' in a 'th' 3D array
$TAB::close_line(); // $num_tr++
$TAB::add_td('Cell'); // saves 'Cell' in a 'td' 3D array
$TAB::display(); // generate the table
When I do this :
$RET='Awesome introduction';
$RET.=$TAB::display();
echo $RET;
... I've got the table before the text.
When I do this :
echo 'Awesome introduction';
$TAB::display();
... I've got the table after the text, and that's correct.
When I generate my table without the class, it's correct too, of course.
Do you know if it's normal (maybe the class' call is executed in first ?) and if I can workaround this ?
I ask because this code is a just a easy sample, the real one is much complex and called with AJAX (and a console.log proves that the table is generated before the text). I hope I'm clear enough.
Thank you.
Yes, probably Completely normal. This Display class is probably doing exactly that. Displaying, not returning values. You are assigning nothing to the $RET.= , You can prove it by echoing it a second time, it will print your header a second time but not your table. At the time that you assign nothing to RET you output the table, probably with print statements or echo statements in the Display class. There maybe be some way to use the eval function to capture the output of the Display class, but that's above my pay grade. (This would just be a comment, but i haven't earned the privilege.)

display data into html table which have special character

Deat All, How do i display data into a html table if my data have like the below:
$str=<table border='1' width='100px'><tr><td>30-00463-00P12><CARDS PENGUIN PICK “UP” HALVES &DOUBLES PK 18><VEST, “RELEASABLE” PACK, ‘MD’, WC W/ ARM</td></tr></table>
I want to display data into a table like "30-00473-03 MAGNETS GEOMAG 216 PIECES GEO76> are misssing.and it does noty print after <> ..i want diaply all character with this special character..Hope you got ..what i want ??
Please help me.
Try using the htmlentities() function in PHP as follows,
echo htmlentities($str);
it will automatically replace special characters by the corresponding HTML entities.
** For more info - htmlentities()

How to combine PHP echo and get together?

My website has multiple languages, and I have a PHP line in below that it get data from sql and it shows text on page:
<?php get_footer_menu_items(3, "col-md-6 go-right","ftitle go-text-right","footerlist go-right go-text-right" );?>
The number 3 in above code is for text (About Us)
But in my page, I have another PHP echo that it show text based on selected language:
<?php echo trans('0295');?>
This above line is for text (About Us)
how can I combine these two lines together that it will change text to selected language?
get_footer_menu_items function probably takes integer as first argument and then as index in an array of some sort with text strings.
So if you call it like this:
get_footer_menu_items(trans('0295'), "col-md-6 go-right","ftitle go-text-right","footerlist go-right go-text-right" );
it won't work anyway.
Without mentioned function's code it's hard to help.

PHP / HTML displaying only x amount of results depending on viewport

I'm using Bootstrap v3 and responsive design works perfectly everywhere besides in this example.
My issue is that when the page is scaled, it always shows 5 divs (as I would expect it to do with the code below), regardless on how many rows it takes up.
$array = array(1,2,3,4,5);
foreach($array as $row)
{
echo "<div style='display: inline-block;'>";
// HTML DIV Content here
echo "</div>";
}
What I want to do, that I can't figure out how is for it to count how many divs will fit in 1 or 2 rows. If 5 can fit in 1 row, then show 5 as 1 row. If 4 can fit in 1 row then show 8 as 2 rows. If 3 can fit in 1 row then show 6 as 2 rows.
So my array would probably contain the max that can be displayed (8 in this example).
I'm not sure if bootstrap can do this out of the box, or if PHP has this functionality. Or if I'm going to have to use jQuery to accomplish this.
Current Behavior:
Expected Behavior:
<table class='table table-responsive
'>
<tr>
<?php
$array = array(1,2,3,4,5);
foreach($array as $row)
{
echo "<td>";
// content here
echo "</td>";
}
?>
</tr>
</table>
Use table responsive instead of div classes and it will render on one line.
Apologies for poor format or errors on my I phone it's never the best experience.
Or if you want to control by size of div it gets harder. You can get same number in each row by echoing bootstrap div classes I.e col-md-3 for example. If you want it to know the widths and your body width is known floats might work.

Selenium, xpath to match a table row containing multiple elements

I'm trying to find a Selenium/PHP XPath for matching a table row that contains multiple elements (text, and form elements).
Example:
<table class="foo">
<tr>
<td>Car</td><td>123</td><td><input type="submit" name="s1" value="go"></td>
</tr>
</table>
This works for a single text element:
$this->isElementPresent( "//table/tbody/tr/td[contains(text(), 'Car')]" );
while this does NOT (omitting the /td locator):
$this->isElementPresent( "//table/tbody/tr[contains(text(), 'Car')]" );
and thus, this obviously won't work either for multiple elements:
$this->isElementPresent( "//table/tbody/tr[contains(text(), 'Car')][contains(text(), '123')]" );
Another way to do this would be with getTable( "xpath=//table[#class='foo'].x.y") for each and every row x, column y. Cumbersome, but it worked... mostly. It does NOT return the <input> tag! It will return an empty string for that cell :(
Any ideas?
This XPath expression:
/html/body/table[descendant::td[contains(.,'Car')]]
Note: If you know your schema, don't use a starting // operator. Use string value instead of text node (this way you get the concatenation of all descendant text nodes).
Several paths can be combined with | separator.
Tweak this:
//tr/td[contains(text(), 'Car')]/text() | //tr/td/input[#value="s1"]/#name
you might want to use
//td[contains,'Car'] and td[contains,'123']/ancestor::tr
that will select the tr that contains td which matches the two contains arguments
Try to use View Xpath Plugin in firefox, very useful plugin.
Learn more about Axes in Xpath: http://www.w3schools.com/xpath/xpath_axes.asp
Thanks to knb for some syntax hints.
This is slightly off-topic, but relevant to the search that led me here...
I had a table with [ name | value ] cells. I needed to get value from the row with 'name' preceding it.
(fake example, but every link I was looking for had the same text and no IDs - the point is that the context information was in a neighboring cell)
<table id="options"><tbody>
<tr>
<td>other</td>
<td>edit</td>
</tr>
<tr>
<td>this label</td>
<td>edit</td> <!-- I want this button -->
</tr>
<tr>
<td>other</td>
<td>edit</td>
</tr>
</tbody></table>
I could retrieve the button I wanted like this, using nested [[]] conditions:
//table[#id='options']/tbody/tr[td[contains(text(), 'this label')]]/td[2]/a
"get the "a" that is in a row that contains another cell with the text I'm looking for"
I think this sort of task might be a common case, so I'm posting it here FYI
In my problem, I had a list of products where it was identified by a unique SKU/catalog combination. If I wanted to add that product to a cart, I chose it by SKU and catalog.
Using foob.ar's example:
//table[#class='foo']/tr[td[contains(text(), 'Car')] and td[contains(., '123')]]
You can combine it with dman's solution for choosing a specific element/column within that row
//table[#class='foo']/tr[td[contains(text(), 'Car')] and td[contains(., '123')]]//input[#name='s1']
Edit:
The solution above works if I was only looking for those two values in any of the columns. If you want to find a value relative to a specific column, I had to modify it a bit
//table[#class='foo']/tr[td[position()=1 and contains(text(), 'Car')] and td[position()=2 and contains(text(), '123')]]//input[#name='s1']

Categories