echo numbers changing them one by one - php

What I am doing:
ob_implicit_flush(true);
ob_end_flush();
$i = 0;
foreach($offers as $key => $value){
i++;
echo 'number: ' . $i;
}
In page it returns like this:
number:1number:2number:3 etc....
What I want to do is make it display only the current number:
number:1, then in second loop change only 1 to 2... so it should display number:2 only... and like that...
Is it possible with only php? I have little skill in javascript, jquery.. All help welcome.

Related

Increment number php every next number

everything good ? I'm having a problem where I'm a beginner in php and would need help.
the user must enter an initial number, for example: 1
<input type="number" name"number" id="number">
where he then informs the quantity: 30
<input type="number" name"qtd" id="qtd">
in case I would like to make a loop in which every 10 repetitions are incremented a new number.
the result of the above example would be:
1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,3,3,3,3, 3,3,3,3,3,3
its possible ?
here look my code that i was trying , but i couldn't find a logical way .
<?php
//ini_set('display_errors', 1);
//ini_set('display_startup_errors', 1);
//error_reporting(E_ALL);
//echo $_POST['sequencial'];
for ($i = 1; $i <= 100; $i++){
echo $i. "<br>";
}
?>
You can do it that way. It works very nice.
$str =array();
for($i=0;$i<10;$i++)
{
for($j=0;$j<10;$j++){
$str[]=$i;
}
$j=0;
}
echo implode($str,',');

Remove the 0 from for results

Currently, I'm messing around with SoundCloud's API and it's returning something that looks like this.
0. HotBox Michael da Vinci Prod Free P.mp3
https://api.soundcloud.com/tracks/373337717/stream?client_id=OURID
1. LSSR Chris P Prod Jake Knight.mp3
https://api.soundcloud.com/tracks/373336760/stream?client_id=OURID
Which is working properly as how the code appears, here it is how I'm printing out the results (very messy but I'm just messing around)
for ($i = 0; isset($house[$i]); $i++) {
$b1 = $house[$i]['title'];
$b2 = $house[$i]['stream_url'];
print '<br>'; print '<br>';
$title = preg_replace('/[^a-zA-Z0-9\s]/', '', strip_tags($b1));
print ''.$i.'. '.$title.'.mp3';
print '<br>';
print $stream = ''.$b2.'?client_id=OURID';
}
Now what I'm wondering is how under every circumstance can we make the 0. return as a 1. and continue counting upwards until reaching the end of the for loop, not removing the 0. data but only changing the number.
I recommend a foreach loop with the $i counter declared in the loop and just increment it as you go.
Untested code:
foreach ($house as $i=>$row){
echo '<br><br>',++$i,'. ',preg_replace ('/[^a-z\d\s]+/i','',strip_tags ($row ['title'])),".mp3<br>{$row ['stream_url']}?client_id=OURID";
}
p.s. I've improved the regex pattern and eliminated all single-use variable declarations. You can break this up over many lines if you prefer.
If you want to avoid the first iteration's double break tags...
foreach ($house as $i=>$row){
if($i) echo '<br><br>'; // if $i is not 0
echo ++$i,'. ';
echo preg_replace ('/[^a-z\d\s]+/i','',strip_tags ($row ['title']));
echo ".mp3<br>{$row ['stream_url']}?client_id=OURID";
}
Based on the comments and your description of what you are trying to do, it sounds like you just need to add another iteration variable:
# Add another variable
$a = 1;
for ($i = 0; isset($house[$i]); $i++) {
$b1 = $house[$i]['title'];
$b2 = $house[$i]['stream_url'];
$title = preg_replace('/[^a-zA-Z0-9\s]/', '', strip_tags($b1));
echo '<br><br>';
# Here you have the $a show up starting at 1
echo ''.$a.'. '.$title.'.mp3<br>';
echo $stream = ''.$b2.'?client_id=OURID';
# Auto increment here
$a++;
}

PHP: Run through multiple URLs and display their content

Let's say we have an URL looking something like this.
http://domain.com/index.php?p=1&u=A1b2C3d4E5f6G7h8I9j
The number span after ?p= goes from 1 to 999 and the rest is unchanged.
Every URL contains one short line of text.
What would a script look like which can run through all 999 URLs and display their contents?
It would be easy. You can use the FOR LOOP.
<?php
for($i=1; $i < 1000; $i++) {
echo file_get_contents('http://domain.com/index.php?p=' . $i . '&u=A1b2C3d4E5f6G7h8I9j');
}
Documentations:
For Loop
file_get_contents() method
This is going to be resource intensive. But taking a shot in the dark, this should work:
<?php
$urlContent = array();
$urlStart = 'http://domain.com/index.php?p=';
$urlEnd = '&u=A1b2C3d4E5f6G7h8I9j';
$count = 1;
while($count <= 999){
$urlContent[$count] = file_get_contents($urlStart.$count.$urlEnd);
$count++;
}
foreach($urlContent as $content){
echo $content.'\n';
}
?>
This code is Untested

Explode and foreach to create multi <td> table

I'm using explode() to basically take apart paragraphs into individual words. Works great. The looping through with a foreach(). Also works great. Nothing complicated here.
$title_pieces = explode(" ", $title_fixed);
foreach($title_pieces as $tpiece){
echo "<b>$tpiece<br>";
}
Unfortunately this returns just an ugly long list of words. What I'd like to do but can't quite figure out how is to put this all in a nice table. Creating the table is no problem, the part I can't figure out is how to get it to write more than one $tpiece per row. I'd like to have maybe 5 <td>s in each row.
So if I do:
foreach($title_pieces as $tpiece){
echo "<tr><td>$tpiece</td></tr>";
}
I'm still just left with a long list. Can someone point me in the right direction here.
Just a sample-code. Work around with the modulo-operator.
<?php
$i = 1;
echo '<table><tr>';
foreach($title_pieces as $tpiece){
if ($i % 10 == 0)
echo "</tr><tr>";
echo "<td>$tpiece</td>";
$i++;
}
echo '</tr></table>';
?>
Try something like:
$count = 1;
echo "<tr>";
foreach($title_pieces as $tpiece){
if ($count % NUM_COLS == 0)
echo "</tr><tr>";
echo "<td>$tpiece</td>";
$count++;
}
echo "</tr>";
There's probably a clever optimization in there. What you are doing is counting the number of cells and starting a new row when the count divides NUM_COLS evenly. It's important count starts on 1, not 0, or you'll have an empty row.

PHP Loop do some once the loop as finished

I have this PHP loop,
foreach($returnedContent as $k => $v) {
$imageName = str_replace($replaceData, "", $v['contentImageName']);
echo "<a class='contentLink' href='".base_url()."welcome/getFullContent/$v[contentId]'>";
echo "<img src='/media/uploads/".strtolower($v['categoryTitle'])."/".$imageName."_thumb.png' alt='$v[contentTitle]' />";
echo "</a>";
}
Once the lopp has finished I was hoping it would be possible to do loop to print x amount of grey boxes is this possible and if so how, basically if the first loop returns 1 item i need the second loop to print out 11 boxes, if the first one returns 9 items I need the second loop to return 3 boxes.
Make sense? Can anyone help me?
So if you want a total of 12 boxes, set a counter and decrement:
$boxes = 12;
foreach($returnedContent as $k =>$v){
// all your previous stuff
$boxes--;
}
for($i = 0; $i < $boxes; $i++){
// print your box here
}
Depending on your application you may also want to check that the number of items in $returnContent is <= $boxes. If it is greater than $boxes you won't get an error but you will get rows with more than $boxes images.
Just keep a counter and increment it for each loop iteration, then add
for (;$counter < 11; ++$counter) {
do_loop_stuff();
}
Maybe you could do something like this (assuming $returnedContent is numerically indexed):
//count to 12 so we get 12 items
for ($i=0; $i<12; $i++) {
//check if there is an entry to print
if (isset($returnedContent[$i])) {
$v = $returnedContent[$i];
$imageName = str_replace($replaceData, "", $v['contentImageName']);
echo "<a class='contentLink' href='".base_url()."welcome/getFullContent/$v[contentId]'>";
echo "<img src='/media/uploads/".strtolower($v['categoryTitle'])."/".$imageName."_thumb.png' alt='$v[contentTitle]' />";
echo "</a>";
} else {
//draw grey box
}
}
After the first loop, you can do:
for($i = 0; $i < 12 - count($returnedContent); $i++)
{
// print the grey boxes.
}
Hmmm Im not sure Im understanding you but
$c = count($returnedContent);
will get you the amount of items in the variable
then:
$c = (11-$c);
if($c > 0) {
for($i=0;$i<$c;$i++) {
// print gray box
}
}
after the first loop. You could also use a counter variable inside the first loop.
I did interpret the question as "Do something when the loop has finished iterating".
In which case a for/foreach loop isn't the best choice here.
how about
<?php
$i = 0;
do {
echo $i;
} while ($i > 0);
//then do whatever else you need to.
?>

Categories