0

I'm trying to show in a div 3 thumbnails stored in different folders.
In my bd determined (with value "1") which record has pictures. So this is my db:

+------+----------------+---------+--------+--------+
| ID   | BRAND          |   PHP   |  RUBY  |  JAVA  |
+------+----------------+---------+--------+--------+
| 1    | ford           |    1    |    0   |    0   |
+------+----------------+---------+--------+--------+
| 2    | seat           |    1    |    1   |    1   |
+------+----------------+---------+--------+--------+
| 3    | fiat           |    1    |    1   |    0   |
+------+----------------+---------+--------+--------+
| 4    | toyota         |    1    |    0   |    0   |
+------+----------------+---------+--------+--------+
| 5    | vw             |    1    |    0   |    1   |
+------+----------------+---------+--------+--------+

Selecting records:

$result = mysqli_query($connecDB,"SELECT * FROM brands WHERE php = '1' OR ruby = '1' OR java = '1' ORDER BY id ASC");


while($row = mysqli_fetch_assoc($result)){
    $new_array[] = $row;
}

But my problem is: How I can select the values ​​"1" and how to know which is which?
Maybe I could use: $row['php'] $row['java'] $row['ruby'] But how to filter the values ​​"0"?

5
  • 1
    tip: simplify your query with WHERE 1 IN (PHP, RUBY, JAVA). Commented Aug 19, 2013 at 19:33
  • Just loop through each one with an if($row['ruby'])... what are you wanting to do if it is a one? Commented Aug 19, 2013 at 19:33
  • @cwhelms With the value "1" say there is a folder with images that correspond to the record. Example: directory php-> folder 1 (id of ford), folder 2 (id of seat), ruby directory -> folder 3 (id of fiat) etc. So should have a "while" or "foreach" with this data and display a url depending on the values ​​"1" in the array: folder/'.$code.'/'.$id.'/ Commented Aug 19, 2013 at 19:41
  • @Jon Perhaps there is some way to simplify many if(php) if(ruby) if(java)? thank you Commented Aug 19, 2013 at 19:49
  • 1
    @santyas: Not really unless I 'm missing something. You can of course hide the conditionals inside a method, which often is a good way to increase both the abstraction and readability of your code at the expense of a tiny performance hit that you probably don't care about (exchanging something you don't need for something you do is a common attribute of good engineering). But the logic still needs to exist somewhere. Commented Aug 19, 2013 at 20:31

2 Answers 2

1

It looks like you want to break up the array into php, java, and ruby. Below is not tested, just to give you an idea.

$resultsArray = array();
while($row = mysqli_fetch_assoc($result)){
    if($row['php'] == 1) {
        $resultsArray['php'][] = $row['id'];
    }
    if($row['java'] == 1) {
        $resultsArray['java'][] = $row['id'];
    }
    if($row['ruby'] == 1) {
        $resultsArray['ruby'][] = $row['id'];
    }
}

Then loop through that 2d array, grabbing the name from the key in resultsArray.

foreach($resultsArray as $language => $array) {
    foreach($array as $id) {
        echo "url/".$language."/".$id;
        echo "<br>";
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Like this? foreach($resultsArray as $key => $val){print_r($val);}
Look at my edit. print_r will only get you the array of an individual language.
Hello again, works perfect. I use it with a single foreach and the id in this way: echo "url/".$language."/".$row['id']; I can ask you something else? Surely you understand the concept and I take long to reformulate a new post. How I can use opendir with directories I got? Thank you very much!
I could write the names of the files that are inside the folders, but I do not know the names. So I need to use readdir @cwhelms
0
foreach($resultsArray as $language => $array){
    foreach(array_slice(glob('work/'.$language.'/'.$rowtest['id'].'/*.jpg'),0,1) as $image){    
        echo $image . "<br />";
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.