Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
http://plnkr.co/edit/06lu6r34eGNfRq1fygNy
I want to only display 10 "cards" from my array, how would I do so?
I tried using unset() but it is very inefficient and doesn't even work for multiple cards.
Also how can I make the code display the "cards" horizontally instead of vertically?
If you’d like to use unset, the correct syntax would be unset($cards[1]); unset($cards[2]); unset($cards[3]); ... and so on.
However, for your particular situation, I’ll recommend you to use array_slice:
$cards = array(
"Messi", "Ronaldo", "Ibrahimovic", "Ribery", "Robben", "Neymar", "Rooney", "Casillas",
"Falcao", "Van Persie", "Hazard", "Iniesta", "Xavi", "Schweinsteiger", "Silva", "Fabregas",
"Lahm", "Aguero", "Cavani", "Vidic", "Ozil", "Mata", "Bale", "ThiagoSilva",
"Kompany", "Tevez", "Toure", "Ramos", "Suarez", "Pirlo", "DiMaria", "Neuer",
"Pique", "Buffon", "Lewandowski", "Gomez", "Chiellini", "Cole", "Pedro", "Busquets",
"Cech", "Muller", "Hummels", "Alonso", "Navas", "Modric", "Cazorla", "Gotze",
"Benzema", "Vidal", "Lavezzi"
);
shuffle($cards);
$cards = array_slice($cards, 0, 10);
For the horizontal display, you may simply omit the <br> at the end of every iteration of your loop, but, depending on the resolution of the user’s browser, the images may occupy more than one line. For a strictly horizontal arrangement, use an HTML table with 10 columns:
print("<table><tr>");
foreach($cards as $card){
$img = "http://d2bm3ljpacyxu8.cloudfront.net/fit/105x97/http://clearpkz.webs.com/webstore/".$card.".png";
print("<td><img src=\"".$img."\"/></td>");
}
print("</tr></table>");
The following works too:
for ($i = 0; $i < 10; $i++) {
echo $cards[$i] ." <br>"; // put the name above the card
echo "<img src='http://d2bm3ljpacyxu8.cloudfront.net/fit/105x97/http://clearpkz.webs.com/webstore/$card.png'> <br>";
}
If you want them horizontally, and without names, don't put in the <br>:
for ($i = 0; $i < 10; $i++) {
echo "<img src='http://d2bm3ljpacyxu8.cloudfront.net/fit/105x97/http://clearpkz.webs.com/webstore/$card.png'>";
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I am using a Rest API to get data from a website and I would like to use that data in my webshop. I have never worked with API's before.
I now have the following code:
<?php
$url = 'https://api.floraathome.nl/v1/products/get?apitoken=[MY_API_TOKEN]&type=json';
$json = file_get_contents($url);
$retVal = json_decode($json, TRUE);
for ($x = 0; $x < count($retVal); $x++) {
echo $retVal['data'][$x]['dutchname']."<br>";
echo $retVal['data'][$x]['purchaseprice']."<br>";
echo $retVal['data'][$x]['promotionaltext']."<br><br>";
}
My problem is that it only shows the first 2 products, but I have 9 products selected. If I print $retVal, it does output all 9 products.
That's why i always prefer foreach(), do like below:-
foreach($retVal['data'] as $retV){
echo $retV['dutchname']."<br>";
echo $retV['purchaseprice']."<br>";
echo $retV['promotionaltext']."<br><br>";
}
Note:- your code will also work if you change count($retVal) to count($retVal['data'])
My problem is that it only shows the first 2 products, but I have 9
products selected. If I print $retVal, it does output all 9 products.
Thats because you are looping over the wrong count of your elements:
for ($x = 0; $x < count($retVal); $x++)
^^^^^^^^^^^^^^
as you can see, the problem is in here count($retVal) which I assume that the structure is like something like that:
Array (
[data] => Array ()
[xxxx] => 'xx'
)
so to solve this, -and if you want to go with for loop rather than the other right solution using foreach- you will need to get the prober count count($retVal['data']):
for ($x = 0; $x < count($retVal['data']); $x++) {
echo $retVal[$x]['dutchname']."<br>";
echo $retVal[$x]['purchaseprice']."<br>";
echo $retVal[$x]['promotionaltext']."<br><br>";
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have a for loop that loops through xml. I then fetch results from the database and look for the id.
What happens is that when an id is found and the break happens, it stops both the while loop and the parent for loop. I just want to stop the while loop. How do I get this to work as intended?
$xml = simplexml_load_file('file/path/here');
$articles = $xml->article;
$total_articles = count($articles);
// Cycle through the list of articles
for($a=0; $a<$total_articles; $a++)
{
// Check if article already exists
// MySQLi select statement goes here
$exists = false;
while($a = $article_result->fetch_assoc())
{
if($a['article_id'] == $id)
{
$exists = true;
break 1;
}
}
}
you're setting $a to $article_result->fetch_assoc().
Here:
...
while($a = $article_result->fetch_assoc())
...
[] < 1 returns false for me, at least for php7.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Well I am trying to make a simple php system.
Anywise I need to separate the text when I want to add it to the database.
So for example I want to add:
abc:123
I want that the : will be the separater, so it'll look like this:
abc
123
And then both will go to a different table.
Could someone help me with this? As I am not an experience PHP coder, yet I am willing to learn how to do this.
Kind regards
This is pretty basic stuff..
$data = explode(':','abc:123');
foreach($data as $word)
{
// some code here
}
Use Split:
<?php
$data = "abc:123";
list ($var1, $var2) = split (':', $data);
echo "Var1: $var1; Var2: $var2;<br />\n";
?>
You can achieve this using explode.
abc:123
Is a string. Let's define it as a variable:
$origin = "abc:123";
You can split the string, using : as the separator.
$separator = ":";
$exploded = explode($separator, $origin);
Now you have an array which you can use to access abc and 123 individually.
$pre = $exploded[0];
$post = $exploded[1];
You don't know how many splits there will be?
That's okay. Your array simply increases, meaning you can simply loop through the array and handle the values.
foreach ($exploded as $split)
{
// Do something with $split
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am trying to get the following code to add how many people can retire and not. the code displays but does not add. What am I doing wrong?
<?php
$canRetire= 0;
$notRetired = 0;
$agesFile = fopen("ages.txt", "r");
$nextAge = fgets($agesFile);
while (feof($agesFile) ){
list($agesFile)=explode(":",$nextAge);
if ($agesFile > 65){
$canRetired = $canRetired + 1;
}
else{
$notretired = $notRetired + 1;
}
$nextAge = fgets($agesFile);
}
fclose ($agesFile);
print("<p>Number of people can retired : $canRetired</p>");
print("<p>Number of people not retired: $notRetired</p>");
?>
Shouldn't it be while(!feof(...))?
You want to stop when you reach the end of file.
Plus it looks like you've got some spelling errors:
$notretired = $notRetired + 1;
Notice how the R is lower case on the left and upper case on the right.
Also at the beginning you have $canRetire and in the if condition inside the loop you have $canRetired.
And just another little hint for you: $notRetired = $notRetired + 1; is the same as $notRetired++;
Made a couple modifications to cases, and what appears to be a type based on the variables you have defined. Looks like the variable $canRetire does not match $canretired.
Using while(feof($fh)) is basically saying, do this for the EOF. Which renders that entire loop useless. Using while(!feof($fh)) will allow you to loop until the EOF.
<?php
$canRetired= 0;
$notRetired = 0;
$agesFile = fopen("ages.txt", "r");
$nextAge = fgets($agesFile);
while (!feof($agesFile) ){
list($agesFile)=explode(":",$nextAge);
if ($agesFile > 65){
$canRetired = $canRetired++;
}
else{
$notRetired = $notRetired++;
}
$nextAge = fgets($agesFile);
}
fclose ($agesFile);
print("<p>Number of people can retired : $canRetired</p>");
print("<p>Number of people not retired: $notRetired</p>");
?>
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
So far I have this
<?PHP include('simple_html_dom.php');
$html = file_get_html('http://www.mangastream.com/');
foreach($html->find('.side-nav') as $t)
foreach($t->find('a')as $k)
echo $k->href . '<br>';
?>
which outputs all the links from inside the class. but I just want to have the first 5 links.
find() returns an array, you can do a single find operation instead of two, and you can slice an array to the first five elements by using array_slice.
This allows you to get the first five element easily:
$ks = $html->find('.side-nav a');
foreach (array_slice($ks, 0, 5) as $k)
echo $k->href, '<br>'
;
However I suggest you take the DOMDocument based HTML parser - perhaps in compbination with SimpleXML so that you can run xpath queries on the document instead.
try that
<?PHP include('simple_html_dom.php');
$html = file_get_html('http://www.mangastream.com/');
foreach($html->find('.side-nav') as $t){
foreach($t->find('a')as $key => $k){
echo $k->href . '<br>';
if($key >= 4){
break;
}
}
}
?>