How to count an array content and assign number position with php - php

am working on a website project in which i fetch array from my database and use foreach statement to output its contents.
I want use php script to count how many items are in the array and assign number position to each item so that if the number position of the first item is an odd number i will apply css float:left style to it but if its even number it will float:right
any help please??.
thank you.

Use the modulus operand:
<?php foreach ( $array as $index => $item ): ?>
<div class="<?php echo $index % 2 ? 'even' : 'odd'; ?>"></div>
<?php endforeach; ?>
Then just use those class names in your CSS:
.odd { float: left }
.even { float: right }

CSS3 has nice features (.list:nth-child(odd) {float:left;} .list:nth-child(even) {float:right;} ), but beware that this will not work on many browsers (ie8 and below, older firefox, etc) .. at least every user with WinXP+IE will see just a normal list without the different colors.
jQuery (also noted here) can also select with $('.xy:odd').css({'float':'left'}); , but if you are not using jQuery in your project its just a big (90kb jQuery library) overhead.
Performance is also better if going with php.
So you better go with php and the modulo operand (if $count % 2), see Joseph Silber's answer here.

I think you can also use jquery with :odd selector.

You can do it with css only:
.list:nth-child(odd) {float:left;}
.list:nth-child(even) {float:right;}

Related

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.

Generating an HTML color code for event category in ai1ec

Although this question relates to a particular Wordpress plugin called the All in One Event Calender by Time.ly it can also be a general PHP related question.
I am trying to modify a theme to use event colours selected in the ai1ec back end and would like to produce a simple HTML colour code - ie "#f2f2f2"
The plugin itself has loads of functions and php shortcodes to pull a wealth of information off each event such as the one listed below.
<?php echo $event->get_category_text_color(); ?> which will print style="color: #f2a011;"
Can also change to print style="background-color: #f2a011;" with the use of $event->get_category_bg_color();
Now the real meat of the question
All I want to do is get that HTML colour so I can also code it into buttons and other visual elements. I have scoured through blogs and code to try and find something that does it to no avail.
What I was wondering is if you could write a filter of some sort to just take the information within the "#f2f2f2" quotation marks. I know it's not called a filter as searches for php filter return information about something completely different - I'm a self taught PHP programmer and so searching for a lot of terms I don't know can be pretty tough!
As pointed above, substr would be a great solution but it wouldn't solve the case where the color code is expressed in this format:
#FFF;
instead of:
#FFFFFF;
Therefore, this regex should do the job quite well:
'/?=#(.*(?=;)))/'
Example:
$matches = array();
preg_match('/?=#(.*(?=;)))/', $event->get_category_text_color(), $matches);
$colorCode = "#{$matches[0]};";
You could use the substr() function like so:
echo substr($event->get_category_text_color(),14,-2);
Which in the example, would return #f2f2f2.

Extracting prices from html using jQuery

I'm parsing an html document and I need to extract all the prices in it (the format is $99.00). So what I want to do is extract all the elements that contain the substring "Price" (or "price") in its class or id attribute. But I tried using something like $("[class*='Price']") or $("[id*='Price']") and then concatenate the results on an array but the jquery selector part is not working properly, is not finding anything. Am I doing something wrong, or is there a better way to do this? Any suggestions for a better approach?
Thank you.
UPDATE:I'm actually using a jQuery port called phpQuery for php.
UPDATE2: I don't know the exact class or id of the elements since this is a generic script that I will run on different e-commerce sites, so that's why I'm using the *= wildcard to get all elements (mostly a, div, span, etc, I don't need input). I figured it out and this is what I have so far:
function getPrice($doc){
phpQuery::selectDocument($doc);
$prices = array();
foreach(pq("[class*='Price'], [class*='price'], [id*='Price'], [id*='price']") as $res){
$each = pq($res);
if(preg_match('/\$\d+(?:\.\d+)?/', $each->text(), $matches)){
echo '<br>'.$matches[0].'</br>';
$prices[] = $each->html();
}
}
}
This is printing the correct elements. Now I need to extract the font-size of those elements so I can sort the array by font-size.
Select by ID: $("#Price")
Select by Class: $(".Price")
Showing your HTML would help.
Give this ago
$(function(){
$('.price').each(function(){
//create your array here of $(this).val() or $(this).html()
});
});

How to number things in PHP?

UPDATE:
I know I can use <ol> directky in the output but I remember using something like:
<?php echo $i++; ?> when I worked on a wordpress blog once. Every time I inserted that tag a number greater than the previous appeared so I basically did:
<?php echo $i++; ?> Text
<?php echo $i++; ?> Text
<?php echo $i++; ?> Text
I'm a front end guy (HTML/CSS) so please excuse this basic question. I just need to know what code in PHP I can use to number some text.
Text
Text
Text
into:
Text
Text
Text
Kind of like what <ol> does in html but in PHP.
Updated answer:
You can use a variable as you already do (the example you are posting should already work). Just initialize it using $i = 0;
Old answer:
You have a fundamental misunderstanding here. PHP is a scripting language, not a markup language. PHP does operations like connecting to data sources, calculating, making additions, changing entries in databases, and so on. PHP code, in short, is a series of commands that are executed. PHP has no design elements, tags and formatting options in itself.
PHP can (and usually does) output HTML (Where you have <ol>) to display things.
You can have an array of arbitrary data in PHP, coming from a file or data source:
$array = array("First chapter", "Second chapter", "Third chapter");
you can output this data as HTML:
echo "<ol>";
foreach ($array as $element) // Go through each array element and output an <li>
echo "<li>$element</li>";
echo "</ol>";
the result being (roughly)
<ol>
<li>First chapter</li>
<li>Second chapter</li>
<li>Third chapter</li>
</ol>
It depends on what type of file you are trying to write. Most often, PHP is writing a webpage in HTML, but not always. In HTML, if you want a numbered list, you should use an ordered list (<ol>).
If you're just writing a text file of some kind, incrementing and outputting a variable (like $i in your example) should work.
You mention Wordpress, so it's worth noting that if you worked on a Wordpress template before, you were using dozens of special functions in the Wordpress library, even though you may not have been completely aware that was what you were doing. A lot of the PHP heavy lifting is hidden and simplified for the templating engine, and if your current project is not built on that engine, you will have to do that logic yourself.

Workaround for floats in DOMPDF

DOMPDF does not support floats.
However I am listing many tables, and they are mainly key & value pairs. I would like 2 of these tables to appear side by side.
i.e. if I could use floats
HTML
<table id="stuff">
...
</table>
<table id="other-stuff">
...
</table>
CSS
table#stuff {
float: left;
}
table#other-stuff {
float: right;
}
What sort of workaround can I do to support this? Or is it impossible?
If anyone has any ideas, there is a place to test here.
It looks like it may be supported in the beta.
You can download that from the Google Code page.
Make a table with 2 cells (50% if needed). Align 1st to left and the 2nd to right.
In each cell you place your tables.
It should look like 2 floats.

Categories