Jump to content

Assign Images to an Array


itskimical

Recommended Posts

Please see: http://www.w3schools.com/php/func_array.aspSo,$group = array("Web_Site"=>"web_site.jpg",...EDIT: How to display your array might be best for another topic. You've asked a very basic array question. It's probably best to get your array question answered first.

Link to comment
Share on other sites

If only the word are difined, then I would make an array inwich each key/word has the url of the image like this:$group_img = array("Web Site"=>"house.png", "E-Store"=>"shop.png", "Advanced"=>"adv.png", "Site Extras"=>"extra.png", "Administration"=>"admin.png");then you do your foreach or while or....and use the name provided by the DB to get the URL of the image $group_img[$name]

Link to comment
Share on other sites

That link will lead you to the answers you need. gar onn even told you how to get access to your array. A better follow-up question might have to do with being able to display your array to be able to check its contents before you try to use it. Do you know how to display the contents of an array? That's something you should know how to do before moving on to all your other questions. Also, please try to stay focused. One question per topic is preferred (but not always possible).

Link to comment
Share on other sites

Confusing. You talk about an array, but the picture you show looks like you are requesting an answer related to HTML and CSS. Do you want to print out the contents of the array (a bunch of text)? Or do you need to know how to display some text and images?

Link to comment
Share on other sites

If you could help me that would be great!!!I have the following array

// Set permission groups$group = array("Web Site", "E-Store", "Advanced", "Site Extras", "Administration");// On what group number should the first set of links end?$linkend = 6;

To display this in a separate file is .....

<?phprequire_once("../stconfig.php");foreach ($group as $linkval => $linkset){if ($linkval < $linkend){$lkquery = "SELECT * FROM " .SB_DB_PREFIX ."_permissions WHERE ";if ($set_master_key == "no")$lkquery .= "GivePermission = 'Yes' AND ";$lkquery .= "SiteGroup = '$linkset' ORDER BY ID";$lkresult = mysql_query($lkquery, $dblink) or die ("Unable to select links.");if (mysql_num_rows($lkresult) > 0){echo "<div>";echo "<span>$linkset</span>";for ($lk=1; $lkrow = mysql_fetch_array($lkresult); ++$lk){$featurename = str_replace(" ", " ", stripslashes($lkrow[Feature]));if ($featurename == "Coupons")$featurename = "Coupons";if ($setpg == $lkrow[SetPg])echo "<a href=\"$lkrow[SetPg].php\" class=\"current\">$featurename</a>";elseecho "<a href=\"$lkrow[SetPg].php\" class=\"links\">$featurename</a>";}echo"</div>";}}}?>

This is the outputcurrentsidebar.gifThis is what I would like for it to look likewantedsidebar.gifThe array is defined in my MYSQL file.

Link to comment
Share on other sites

If you want to store more information about each element in the array, then you'll need to use nested arrays. Each element in the array will be another array with the information you want. e.g.:

$group = array(  array('text' => "Web Site", 'image' => 'website.jpg'),  array('text' => "E-Store", 'image' => 'e-store.jpg'),  ...);

Then you need to also change the loop that processes the array. In this line:foreach ($group as $linkval => $linkset)That used to set the $linkset variable to be the title to show, but with the new array structure now $linkset is an array, not just a string of text. So, inside the loop where you use $linkset in the SQL query, you'll need to replace those with $linkset['text'] so that it still uses the text in the menu. Where you print the link tags, however you want to show the images in the HTML, you can get the filename in $linkset['image']. So you may want to add an img tag with the filename as the src, or you may want to add some inline CSS to the link tags to specify a background image with the filename and pad the text over to the right. But, however you want to display the image, you can get the filename in $linkset['image'] inside the loop. The other style changes (borders, colors, etc) can all be replaced in the CSS code.

Link to comment
Share on other sites

i think what he's trying to show is that you make an array for each menu item, containing its text and image src. All those grouped together constitute an array in and of itself. It's essentially like recreating JSON but for array syntax. i.e.

$menu = {   "home"	:	{ 	   "text"		:   "Home Page",	   "image"	 :   "images/home.jpg"   },   "about"   :	 {	   "text"		:   "About Page", 	   "image"	 :   "images/about.jpg"   },   //etc}

rather than one, long, unorganized array. The grouping provides for better distinction, organization, and clarity. And also works much better when used with a (foreach) loop.

Link to comment
Share on other sites

justsomeguy, wouldn't a simple associative array do the same job?
The original code expects the array index to be numeric, it treats the index as a counter. Regardless, even if an associative array would work, a nested array is going to give more flexibility if you want to add a third or fourth piece of information for each item.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...