Page 1 of 1

in_array()

Posted: Sun Jun 29, 2008 2:58 am
by Jim_Bo
Hi,

I have a field called "types" that holds record using , as delimiter. When I loop through the records I have tried to explode the type field and look using in_array for a match, for each match found show an image ..

The code I have below seems to image for every record even tho only some of the records have a match. Here is what I have:

Code: Select all

    $numcols = 3;
    $numcolsprinted = 0;
 
    $sql = mysql_query("SELECT * FROM portfolio");
    
    echo '<table width=100%" align="center" border="0" cellspacing="2" cellpadding="2">';
    
    while($row = mysql_fetch_array($sql)) {
        
    if($numcolsprinted == $numcols) { 
    echo "<tr></tr>";
    $numcolsprinted = 0;
    }
    
    if(isset($_SESSION['userid'])){
    $delportf = '<br /><font size="1"><a class="second" href="'.$_SERVER['PHP_SELF'].'?action=addportfolio">Add</a> - <a class="second" href="'.$_SERVER['PHP_SELF'].'?action=addportfolio&edit=edit&portfolioid='.$row['portfolioid'].'">Edit</a> - <a class="second" href="'.$_SERVER['PHP_SELF'].'?action=deleteportfolio&portfolioid='.$row['portfolioid'].'">Delete</a></font>';
    }
    
    echo '<td align="right" bgcolor="#DCDCD1">';
    
    $types = explode(",",$row['types']);
    foreach($types as $type) {
    if(in_array("Print", $types)) {
    $w =  'link to print image here';
    }
    if(in_array("Website", $types)) {
    $p =  'link to website image here';
     }
    }
    
    echo ''.$w.' '.$p.'<img src="./images/portfolio/multimedia.gif" />&nbsp;<img src="./images/portfolio/web.gif" />&nbsp;<img src="./images/portfolio/cms.gif" />&nbsp;<img src="./images/portfolio/ecommerce.gif" />';
    echo '</td>';
    echo '<td valign="top" width="25%" bgcolor="#DCDCD1">';
    echo "<b><a class=\"second\" href=\"javascript&#058; AjaxRequest('main','./includes/portfoliodisplay.php?id=".$row['portfolioid']."')\">".$row['name']."</a> ".$delportf."</b>"; 
    echo '</td>';
 
    $numcolsprinted++;
    }
    
    $colstobalance = $numcols - $numcolsprinted;
    for($i=1; $i<=$colstobalance; $i++) {
    } 
    
    echo "</table>";
 
How would I loop through it so it shows the images next to each record dependant on the in_array match for each record?

Thanks

Re: in_array()

Posted: Sun Jun 29, 2008 7:45 pm
by Jim_Bo
Hi,

Any ideas? I thought this would be easy as, I assume its something simple I am missing?

Thanks

Re: in_array()

Posted: Sun Jun 29, 2008 10:26 pm
by Stryks
I cant put my finger on why exactly, but I find that code really hard to read.

Anyhow, I'm assuming that the issue you are referring to is in this section ....

Code: Select all

   $types = explode(",",$row['types']);
    foreach($types as $type) {
    if(in_array("Print", $types)) {
    $w =  'link to print image here';
    }
    if(in_array("Website", $types)) {
    $p =  'link to website image here';
     }
    }
I don't think this is the problem, by the foreach line in there is redundant given the in_array approach. Remove it first, and then add ...

Code: Select all

print_r($row['types'])

... just before the first line of the snippet above and post back with what it contains.

Re: in_array()

Posted: Mon Jun 30, 2008 3:36 am
by Jim_Bo
Image

Travel, Bio Care and Construction are headings, on the left to each heading the topline is print_r($row['types']), under that the variables $p and $w are printed. Under "Travel" there is a match for "Website and Print", under "Bio Care" there is only a match for "Website" but it prints both varibles for that and every other record. I am trying to make it print only the variables for each record if there is a match in the array.

So under Travel it should print "Web - Print" under Biocare "Web" and Construction only "Web" etc etc

Re: in_array()

Posted: Mon Jun 30, 2008 6:18 am
by Stryks
Well, OK.

Your primary problem is that you set $p and $w when they are found, but don't reset them when they are not. So, first time around both are found, and with nothing to reset them, they just stay set and output positive for all. To see what I mean, have your first entry with neither web nor print, the second with print only, the second with web only, and the fourth with neither.

I'm betting the first and second display correctly, the last two will show all.

My main question is though .... why?

You have a database. Why not just make a field for each possibility, and then when displaying the field, say ...

Code: Select all

if($row['website'] != "") echo "<a href=" . $row['website'] . ">Website</a>"
That way, the trigger to show the link depends on the actual URL being specified.

But if you prefer ...

Code: Select all

   $types = explode(",",$row['types']);
    if(in_array("Print", $types))   $w =  'link to print image here';   else $w = "";
    if(in_array("Website", $types)) $p =  'link to website image here'; else $p = "";
 
I gotta say though ... it feels messy.

Cheers

Re: in_array()

Posted: Mon Jun 30, 2008 7:35 pm
by Jim_Bo
Hi,

Thanks, both ways work ..

I thought taking the explode aproach would be easyer given nothing in the script will be expanded on.

Thanks!