Skip to content
Advertisement

Positioning of arrays

Is it possible to position an array value depending on a specific numerical array? I have this table:

+---------+-------+-------+------------+---------------------+------------------------------+
| deck    | count | break | cards      | donator             | image                        |
+---------+-------+-------+------------+---------------------+------------------------------+
| prejoin | 20    | 5     | 16, 01, 05 | User2, User1, User7 | img2.jpg, img1.jpg, img7.jpg |
+---------+-------+-------+------------+---------------------+------------------------------+

and this is what I have so far: https://shizen.hakumei.org/test.php

It shows only User7’s name and image link where I plan to show box 01 for User1’s name and image link then box 16 for User2’s name and image link. So basically like:

Box 16 = User2 (img2.jpg link)
Box 01 = User1 (img1.jpg link)
Box 05 = User7 (img7.jpg link)

This is the code I currently have to display the table from the given link:

function trim_value(&$value) { $value = trim($value); }
$res = $database->query("SELECT * FROM `table` ORDER BY `deck` LIMIT 1");
while ( $col = mysqli_fetch_assoc($res) ) {
    echo '<center>';
    $cards = explode(',', $col['cards']);
    array_walk($cards, 'trim_value');
    if ( $col['cards'] == '' ) { $count = 0; } else { $count = count($cards); }

    $donator = explode(',', $col['donator']);
    $donator = array_map(trim, $donator);
    foreach ( $donator as $usr ) { $usr = trim($usr); }

    $image = explode(',', $col['image']);
    $image = array_map(trim, $image);
    foreach ( $image as $img ) { $img = trim($img); }

    echo '<h2>'.$col['deck'].' ('.$count.' / '.$col['count'].')</h2>
    <table width="625" cellspacing="0" border="1"><tr>';
    for ( $i = 1; $i <= $col['count']; $i++ ) {
        $number = $i;
        if ( $number < 10 ) { $number = "0$number"; }
        $card = $number;
        if ( in_array($card, $cards) ) echo '<td width="125" align="center" height="105">'.$card.'<br ><a href="'.$img.'" target="_blank">'.$usr.'</a></td>';
        else { echo '<td width="125" align="center" height="105">00</td>'; }
        if ( $col['break'] !== '0' && $i % $col['break'] == 0 ) { echo '</tr>'; }
    }
    echo '</table>
}

Many thanks in advance! If it’s not possible or if there’s any better way to do it, I’m open to ideas as well. 🙂

Advertisement

Answer

A tip: You don’t pay rent for space in your code, so you should use it for better scanability.

I have put your usernames and your images in a key based array, so you can just look up, if that key is existing, if not you don’t need a username or an image. Problem here is your storing in the table, that you use numbers with 0 at the begin.

You should also think about this: Delete the columns card, images, donator and put all values in a different table for example:

deck: prejoin, card_id: 16, user: 17, image: img12.jpg

while ( $col = mysqli_fetch_assoc($res) ) {
    $data = array();

    if ($col['cards'] == '' ){
        $cards = explode(',', $col['cards']);
        array_walk($cards, 'trim_value');
        $count = count($cards);

        $donator = explode(',', $col['donator']);
        $image = explode(',', $col['image']);
        foreach ($cards as $key=>$card){
            $data[$card] = array(
                'user' => trim($donator[$key]),
                'img' => trim($image[$key])
            );
        }
    }

    echo '<h2>'.$col['deck'].' ('.$count.' / '.$col['count'].')</h2>
    <table width="625" cellspacing="0" border="1"><tr>';
    for ( $i = 1; $i <= $col['count']; $i++ ) {
        $number = $i;
        if ( $number < 10 )
             $number = "0".$number;

        if ( in_array($i, $data) )
            echo '<td width="125" align="center" height="105">'.$i.'<br ><a href="'.$data[$i]['img'].'" target="_blank">'.$data[$i]['user'].'</a></td>';
        else
            echo '<td width="125" align="center" height="105">00</td>';

        if ( $col['break'] !== '0' && $i % $col['break'] == 0 )
            echo '</tr>';
    }
    echo '</table>';
}
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement