I'm trying to create Contest list of users in php.
But my code style don't give me what I want
My code:
<?php
$users = ["129292199", "616161616", "272727272", "555554433", "666665886"];
$count = 0;
foreach ($users as $name){
$count += 1;
echo "{$count} $name\n";
}
?>
My code output
1 129292199
2 616161616
3 272727272
4 555554433
5 666665886
But I want something like this
š 1st : 5453674081
š„ 2nd : 135678975
š„ 3rd : 5607170108
4ļøā£ 4th : 1460057759
5ļøā£ 5th : 1664868970
you can wrap the count variable inside a class and style it with css
Related
I know how to count elements inside a loop, but tried and canĀ“t find a solution. Actually, I can count the elements in loop in this way:
$i = 0;
foreach ($Contents as $item)
{
$i++;
}
echo $i;
But in my case, I have large function and I can do this, but need more time for count elements inside. I canĀ“t put the string for showing number elements inside loop outside of the function. I must show inside loop all number of elements and show as this. Here's an example:
$i = 0;
foreach ($Contents as $item)
{
print "Number Elements itĀ“s : $i";
print "Element ".$i."<br>";
$i++;
}
The problem here is that the phrase "number elements itĀ“s" always repeats, because inside loop, and all time $i show me 0,1,2,3,4,5 ...... and all time the same as number of elements inside loop.
My idea and question is if itĀ“s possible inside loop to show the number of elements and after this show the elements as this structure when executing the script:
NĆŗmber elements itĀ“s : 12
Element 1
Element 2
Element 3
.
.
.
.......
This itĀ“s my question. I hope you understand it all. Thanks in advance.
As mentioned in the comments, youu will need to use the count() function for that. If you insist on having it inside the loop, simply do it like this:
$i = 0;
foreach ($Contents as $item)
{
if($i == 0) {
print "Number of Elements is : " . count($Contents) . "<br><br>";
}
print "Element ".$i."<br>";
$i++;
}
I have a database with "7" user account objects.
I have called the objects using any of the loop functions.
foreach (objects as object) {
echo '<div class="col-4>"'.object->property.'"</div>';
}
I want to add rows for each 3 columns as they are being displayed.
The above code would result in all my objects/colums/divs being nested in one row.
I want a way in which I can have the loop adding a row for every 3 columns.
Count the objects as you go. If $x divided by 3 has no remainder, close and open the next div:
$x = 0;
foreach ($objects as $object){
$x ++;
echo '<div class="col-4">"'.$object->property.'"</div>';
echo ($x % 3 == 0) ? '</div><div class="row">' : '';
}
I'm a beginner and trying to convert static data template to dynamic data.. Now I am facing a problem, where I have to use a for each loop to fetch data from array then display on certain divs..
There are 3 similar div containers but the third one has an additional class to move it to the right .. for eg: (div1 => clas= "hello", div2 => class = "hello" , div3 => class = "hello floatrt" >) .
The problem is my data is displayed but due the absense of class ="floatrt". My div's are being displayed vertically. but according to the static template design they are in a single row ..
How can I add class to the second or third or additional divs that are being generated from database.. but not the first div..
Try this one
$count = 1;
foreach ( $array as $k => $v ) {
// do anything here
if ($count % 3 ==0) {
echo '<div1 clas= "hello floatrt" ></div>';
} else {
echo '<div1 clas= "hello" ></div>';
}
$count++;
}
This question already has answers here:
How to detect duplicate values in PHP array?
(13 answers)
Closed 7 months ago.
In php,I have one ArrayList. Lets say
$list = {1000,7000,5000,1000,6000,5000,1000,2000};
So what I want to do is that Make count of each element in list:
For example as above ,
1000 comes three times then count of 1000 = 3
5000 comes two times then count of 5000 = 2,etc.
And I want to access that count of different elements separately.
Edit:
Here I have for loop
for($i=0;$i<count($trip);$i++)
{
// Here list will be new everytime for loop run.
$list = $Users->Matches($trip[$i]);
}
// ----------------------------------------------------------------------
Here what I want to do is that "Take all the element of list for value of
$i : 0 to count($trip)" and "add it into another list lets say $allList
and access" as below :
// -----------------------------------------------------------------------
$cnt_arr = array_count_values($allList);
foreach($cnt_arr as $key => $value) {
echo "\n\n Count of ".$key." = ".$value." ";
}
OUTPUT :
Lets say for loop runs first time :
$list = {1000,7000,5000}
Now for loop second time :
$list = {8000,1000,9000}
Now for loop is completed and at the end I want the $allList value as below :
$allList = {1000,7000,5000,8000,1000,9000}(which has all the elements of above-appended list)
So How can I do this ?
Please Guide me. Any help will be appreciated.
Try with array_count_values like
$cnt_arr = array_count_values($list);
foreach($cnt_arr as $key => $value) {
echo "Count of ".$key." = ".$value."<br>";
}
See this LINK
As per your edit,You need to store them like array like
for($i=0;$i<count($trip);$i++)
{
// Here list will be new everytime for loop run.
$temp_list = $Users->Matches($trip[$i]);
foreach($temp_list as $tmp) {
$list[] = $tmp;
}
}
I have a data set from mysql in a multidimensional array.
I am using a pagination script to show 10 per page of that array. I want to know how to append a number to each one like a scoring system. The array is sorted by the way that it will aways be so i want to to add a 1 to the first item, a 2 to the second item and so on.
I dont want to do this in the foreach that i output since this will not translate over to the second page where it would start back at 1.
Any ideas on how to attach a number in desc order to each item in the array so i can access it in the foreach and display it?
Thanks in advance
Use the same math used to find the offset in your pagination query and start counting from there.
fist you need to save the indexes in a $_SESSION variable:
$_SESSION['indexes'] = array();
and for multidimentional:
foreach( $array as $index=>$arrValue) {
echo $index;
foreach($arrValue as $index2=>$value){
echo $index2;
$_SESSION['indexes'][$index][$index2] = $value;
echo $value;
}
}
than you can go through all of the session indexes; where $index is the page or $index2 can be the row
I figured it out by doing some calculations based on which page i was on and the page count that was set and finding the size of the array from the db:
$all_members = get_users_ordered_by_meta('metavalue', 'desc');
$array_count = count($all_members);
$posts_per_page = get_option('posts_per_page');
if ($page > 0) {
$total_posts_avalible = ($posts_per_page * $page);
$usernum = $total_posts_avalible - $posts_per_page;
}
Then i echo the $usernum in the foreach next to the name of the user.
foreach() {
$usernum++;
echo $usernum; echo $user_name;
}