Simple PHP question

This is a discussion on Simple PHP question within the alt.comp.lang.php forums, part of the PHP Programming Forums category; I want to be able to randomly select the following from an array: 1). An image 2). A piece of ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 2 Weeks Ago
Ronald Raygun
 
Posts: n/a
Default Simple PHP question

I want to be able to randomly select the following from an array:

1). An image
2). A piece of text (name of tge image)
3). A piece of text (description of the image)


I want to be able to build a static array with the values hardcoded into
the array, and then be able to randomly select an item from the array
and retrieve the image, name and description.

I am new to PHP, but have been programming C/C++ for over 10 years.

In C++ it would look something like this:

class ImageInfo
{
public:
ImageInfo(const std::string& path, const std::string& name, const
std::string& descr);
ImageInfo(const ImageInfo&);
~ImageInfo();

std::string PathName() const ;
std::string Name() const ;
std::string Description() const ;

private:
std::string m_path, m_name, m_descr;
}

static std::vector<ImageInfo> theArray ;


static const ImageInfo& getRandomImageInfo()
{
//generate a random number less than number of items in vector
size_t index = random_index(theArray.size());
return theArray[index];
}

I need to know how to translate this into PHP. I know PHP does not have
the equivalent of the STL, so I will have to use use arrays instead. Any
help will be much appreciated.

Reply With Quote
  #2 (permalink)  
Old 2 Weeks Ago
countach
 
Posts: n/a
Default Re: Simple PHP question

En las nuevas, Ronald Raygun escribió:
> (...)
> I need to know how to translate this into PHP. I know PHP does not have the
> equivalent of the STL, so I will have to use use arrays instead. Any help
> will be much appreciated.



(Best view in fixed width font)

$images = array(
[1] => array(
'path' => '/whatever',
'name' => 'TheName',
'descr' => 'TheDescr')
),
[2] => array(
'path' => '/whatever',
'name' => 'TheName',
'descr' => 'TheDescr')
),
[3] => array(
'path' => '/whatever',
'name' => 'TheName',
'descr' => 'TheDescr')
),
....
[n] => array(
'path' => '/whatever',
'name' => 'TheName',
'descr' => 'TheDescr')
),
);



To get name #3...

echo $images[3]['name'];


The list start in #1, but you can start by 0 or any number or even a
text identifier.


Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 03:42 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0