Hello,
When I echo my articles on a webpage I need to set random style to them, to do so I need to add random class to my <div>
This is what I've got so far - $style_array = array("story_style1", "story_style2", "story_style3", "story_style4");
-
$rand_style = array_rand($style_array);
-
-
$storyDisplayList .= '<div class="' . $rand_style . '"></div>
-
Then I echo $storyDisplayList somewhere on my page to show articles. I thought that by random class to <div> in StoryDisplayList I could set different styles, but instead of giving it a random class of story_style1 it gives it a class of 0 or sometimes array.
Does anyone here has any idea of how can I fix it.
this would have been the short, unexplained answer, which I had in mind in post #4. - $style_array = array("story_style1", "story_style2", "story_style3", "story_style4");
-
$storyDisplayList .= '<div class="' . $style_array[array_rand($style_array)] . '"></div>
26 3220
but instead of giving it a random class of story_style1 it gives it a class of 0 or sometimes array.
but that is what array_rand() is supposed to return.
from the manual: Return Values
If you are picking only one entry, array_rand() returns the key for a random entry. Otherwise, it returns an array of keys for the random entries. This is done so that you can pick random keys as well as values out of the array.
Hi, thnx, but is there a way to randomly select those "story_style1 etc." from array? nothing else just those defined classes I want to use?
thnx
what about $style_array[array_rand($style_array)]; ?
Hi,
I tried what you suggested, but still instead of setting class of story_style1, story_style 2 etc... it gives class of "Array" to all div's.
The Code: -
-
$style_array = array("story_style1", "story_style2", "story_style3", "story_style4");
-
$style_array[array_rand($style_array)];
-
-
$storyDisplayList .= '<div class="' . $style_array . '"></div>
-
-
-
-
//And somewhere in the <body> of the page I echo $storyDisplayList to show all articles.
-
-
In case if I sound confusing here is what I want to do again:
I want to select random class from this list "story_style1", "story_style2", "story_style3", "story_style4"
And put it in the $story_style variable so I can than place it in a place of class <div class="' . $story_style2 . '"></div> and have different designs for content that is in that div.
Thank You
I understood what you intend, but could you explain what line #3 is supposed to do?
besides that, if you echo out an array it prints "Array".
Alright ))) So I thought that by using line 3 I could get random value (story_style1, as an example) from the array in line 2.
So than the variable $style_array could be placed as the class of the div, so when viewed in browser that div would have a class of story_style1. And so on, each div on the page would have it's random class selected from line 2, so I can give them 4 different styles.
Right now the CODE I used above is not working, it gives class of Array ( <div class="Array"></div> ) to all div's on the page.
Thank You
you think too much (no offence meant, but a script doesn’t think either, it just does what it is told to do)
the variable $style_array is defined on line #2 and not changed throughout the entire script (shown). why should it then at line #5 suddenly contain a string?
Uff, I don't quiet understand what you mean. Well I guess, by including it on line #5, $story_array it should than show in the browser like that div has a class equal to one that is in array on line #2. I tried removing line #3 from the code, but the code is still giving me <div class="Array"></div> instead of <div class="story_style1"></div> or <div class="story_style2"></div> etc.
the point is, line #3 does nothing. you fetch a random element from the array – and dump it in the void.
compare the following: - var_dump($style_array[array_rand($style_array)]);
-
var_dump($style_array);
So, I don't need line 3 at all? I tried removing it so now I only have -
$style_array = array("story_style1", "story_style2", "story_style3", "story_style4");
-
But this gives me the output of <div class="Array"></div>.
Do I need to use array_rand function? How can I select values of those story_style1, story_style2, story_style3 etc. and place them as a class??? I'm really confused now, sorry for tones of my questions, but I can't figure it out. I followed the manual on this: http://php.net/manual/en/function.array-rand.php
But it didn't work for me. Could you please suggest a right method that can achieve the task? I mean is it array_rand or I should be using something else?
Thank You
your problem is nor array_rand(), your problem is understanding variables and assignments (sorry to be that bold).
as you already know, array_rand() selects a random key and I assume you know how to get an array value by means of its associated key.
the thing where I can’t follow you is $storyDisplayList .= '<div class="' . $style_array . '"></div>'; and why you expect this to give you for instance "story_style1". $style_array is an Array, period. why should it be "story_style1"?
Right,
I looked at manual again http://php.net/manual/en/function.array-rand.php
And I'm trying to understand it...
Will this work: -
$style_array = array("story_style1", "story_style2", "story_style3", "story_style4");
-
$style = array_rand($style_array , 2); //Manual Has this number 2, What is it suppouse to do here?
-
Here, I assume that $style will have one of the values from array in $style_array.
So I can now include it in my display list -
$storyDisplayList .= '<div class="' . $style . '"></div>';
-
And If I'm right, when I echo $storyDisplayList it should show as <dic class="story_style1"></div>
I think my problem could be (as you mentioned) in understanding how to get an array value by means of its associated key. I think this is a part which I'm stuck with. How exactly does array_rand() give a key to the array and how to use it?
Thank You
- $style = array_rand($style_array , 2); //Manual Has this number 2, What is it suppouse to do here?
because in that particular example, they wanted 2 random keys.
cf.
Description
mixed array_rand ( array $input [, int $num_req = 1 ] )
Picks one or more random entries out of an array, and returns the key (or keys) of the random entries.
I think my problem could be (as you mentioned) in understanding how to get an array value by means of its associated key. […] How exactly does array_rand() give a key to the array and how to use it?
the one has nothing to do with the other. for a description of arrays check its manual section.
Ok I see,
So now when I went through that manual I think I understand what key's are.
So i'm not sure, but is this correct? -
$style = array("styles" => "story_class1", "story_class2", "story_class3", "story_class4");
-
-
//And then in a displaylist ...
-
-
$storyDisplayList .= '<div class="' . $style["styles"] . '"></div>
-
Will this achieve the task, if I have 4 values for one key, does this mean that it would randomly take one of those four? Update:
I tried it out, it works, but always gives me story_class1, now I only need help in figuring out how to display other 3 values from an array, would I use a key on every value and than somehow random it?
Thank You
to check the keys and their associated values, check the output from print_r().
I tried this: -
$style_class = array("styles" => "story_style1","story_style2","story_style3","story_style4");
-
-
$div_class = print_r($style_class);
-
-
$storyDisplayList .= '<div class="'. $div_class .'"></div>
-
And it gives this output: -
Array
-
(
-
[styles] => story_style1
-
[0] => story_style2
-
[1] => story_style3
-
[2] => story_style4
-
)
-
Am I suppose to somehow use it to achieve the task?
Thnx
actually, it doesn’t matter what keys you use, because array_rand() will select one of the available (regardles whether they are numeric or not).
I am really stuck and confused now...
So I will not use the print_r();
Instead I'll do this -
$style_class = array("styles" => "story_style1","story_style2","story_style3","story_style4");
-
$div_class = array_rand($style_class);
-
$storyDisplayList .= '<div class="'. $div_class .'">
-
This gives output of <div class="styles"></div>
So what Am I suppose to do to get final result of - <div class="story_style1"></div>
-
<div class="story_style3"></div>
-
<div class="story_style2"></div>
-
<div class="story_style4"></div>
-
<div class="story_style3"></div>
-
<div class="story_style1"></div>
-
<div class="story_style4"></div>
-
-
and so on ...
-
I just don't understand what I need to do now...
Thnx
print_r() is only for debugging purposes.
Yeh, I understood that when I saw the output, but do you have any suggestion of what I should do next? You can see what I have now in my previous post, I'm now stuck with next step, I have no idea what to do to complete it. This feature is one of the main that I need for my website design, so I really need to understand and get it working... So if you have any suggestions or could point where the issue is could you please help me?
Thank You
I know how to do it, it’s just that I am not the best teacher. and I deem teaching necessary, as this problem turned out to be a very basic one. i.e. giving you the answer would solve the case, but you wouldn’t have a clue what went wrong in the first place.
maybe we should have an IM session later, as there explanation goes smoother.
That would be great, you have been great help, I think that's mee who got things wrong I just got messed up and confused my selves, usually I understand where the problem is when I ask it here, but this is something new in PHP that I don't know yet.
If you have free time it would be great to chat, do you have skype? I don't want others to see my nick name here so if you could e-mail me your contact details …
Hi ;D
It took me a while of research, but I think I figured it out ))) -
$style_class = array("story_style1","story_style2","story_style3","story_style4");
-
-
$random_class = array_rand($style_class, 1);
-
-
var_dump($random_class);
-
-
$div_class = $style_class[$random_class];
-
-
$storyDisplayList .= '<div class="' . $div_class . '"></div>
-
It gives me output that I wanted in my source code, now div's have random classes that I want, but for some reason that I don't understand :/ I get this thing int(1) int(2) int(0) int(3) int(3) int(2) int(3)
On the top of my page, http://inelmo.com < you can see what I mean there. UPDATE: I also remembered that I echo out all errors while I'm in the process of creation
e.g -
error_reporting(0);
-
ini_set('display_errors', '1');
-
Could this be what is causing the problem? As I am not sure whether that is an error or output.
It gives me output that I wanted in my source code, now div's have random classes that I want, but for some reason that I don't understand :/ I get this thing
int(1) int(2) int(0) int(3) int(3) int(2) int(3)
that is the output generated by var_dump() (line #5)
Ohhhh ;D Finally what a relief, it work's now just As I wanted. Thank You Very Much For Awesome help and explaining I really appreciate that! Sorry for taking so much time (25 posts) ;D .
this would have been the short, unexplained answer, which I had in mind in post #4. - $style_array = array("story_style1", "story_style2", "story_style3", "story_style4");
-
$storyDisplayList .= '<div class="' . $style_array[array_rand($style_array)] . '"></div>
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Andrew |
last post by:
When I use a host file entry to point to the host header of the site
that I have created on a Win2k server running IIS5, a cookie is not
created to hold the session id. The result is that...
|
by: Ryan McGeary |
last post by:
In a <select> drop-down, the onchange event isn't called when
scrolling through the dropdown using the mouse-wheel and when crossing
over a new <optgroup>.
Using the example below, notice how...
|
by: Fieldmedic |
last post by:
I'm trying to determine the best way to randomize a set of array items. I'm
thinking that I could use an arraylist and have it use the lower and upper
bounds as the limits. Any suggestions?
...
|
by: Jan Killian |
last post by:
Hi all,
maybe you'll see the trick and could help me. I am passing int by
reference and get an exception when using it as an index to passed
array. (Yes I know I can workaround with object...
|
by: David Berman |
last post by:
It seems that my site is losing session information when using
Server.Transfer. I have a page called PictureGallery.aspx. It takes an
argument which is an index id, so it would look like...
|
by: hali |
last post by:
Hi all
I'm working on jsf & I have a problem displaying a list of Items via hand set
my code is:
<wap:dataTable value="#{predefinedESP.esps}" var="esp" >
<wap:column >
...
|
by: hali |
last post by:
Hi all
I'm working on jsf & I have a problem displaying a list of Items via hand set
my code is:
<wap:dataTable value="#{predefinedESP.esps}" var="esp" >
<wap:column >
<waputputText...
|
by: jeremy |
last post by:
Had a tough time figuring this one out and couldn't find a good
solution, so I thought I would post this and hopefully it will help
someone out.
When using DataBind to dynamically bind a list to...
|
by: sail0r |
last post by:
Perhaps this is obvious but I am not sure what is going on...
Here is the relevant code:
char *command;
char *argument;
char url="file:///usr/u/myname/Project/cats/";
char...
|
by: itdevries |
last post by:
Hi,
I've ran into some trouble with an overloaded + operator, maybe
someone can give me some hints what to look out for.
I've got my own custom vector class, as a part of that I've overloaded...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |