0

Currently, the below code is displaying the $ids array...

how can I echo only 1 id from it? I want to echo each id differently and not all at once. right now its echo's all 4 ids at once. I wish to echo them individually its working like echoing all emblem images mentioned in the $ids. i want them to be echo individually and not by foreach i want it to be echo something like

emblem($guild->guild_id of $ids=1);?>

emblem($guild->guild_id of $ids=2);?> emblem($guild->guild_id of $ids=3);?> and so on...

below is the code,

    <?php 
    $castleNames = Flux::config('CastleNames')->toArray();
    $ids = "1,2,3,4";
    $sql  = "SELECT castles.castle_id, castles.guild_id, guild.name AS guild_name, guild.emblem_len FROM {$server->charMapDatabase}.guild_castle AS castles ";
    $sql .= "LEFT JOIN guild ON guild.guild_id = castles.guild_id ";
    $sql .= "WHERE castles.castle_id IN ($ids)";
    $sql .= "ORDER BY castles.castle_id ASC";
    $sth  = $server->connection->getStatement($sql);
    $sth->execute(array_keys($castleNames));
    $castles = $sth->fetchAll();
    ?>
    <?php if ($castles): ?>
    <?php foreach ($castles as $guild): ?>
        <div class="flag">
            <img width="24" src="<?php echo $this->emblem($guild->guild_id) ?>">
        </div>
    <?php endforeach ?>
    <?php endif ?>

Thanks in advance note i have a very very basic knowledge of phpmysql, very beginner stage

johny
  • 9
  • 3
  • Sorry, not able to understand clear requirement. Do you want to print all images into different rows. In provided code you already interating loops so all id should print one by one. Share schema will sample data. Are you facing problem for display data or fetching data? – Dipti Dec 17 '18 at 05:24
  • 1
    Basically, foreach will loop through every element of the array. Your code will print each element when it goes through it. If you want to print 1 element only, you have to know exactly which element you want to print and its index. –  Dec 17 '18 at 05:35
  • Possible duplicate of [How can I access an array/object?](https://stackoverflow.com/questions/30680938/how-can-i-access-an-array-object) – But those new buttons though.. Dec 17 '18 at 05:55
  • @Dipti i am not facing problem of fetching, in short i just want to remove the foreach and display them 1 by 1 . . for example if $ids = 1 emblem($guild->guild_id);?> so that it will display emblem of id 2 and so long. at the moment in foreach it is display all emblems of all ids 1,2,3,4 – johny Dec 17 '18 at 06:02
  • write one more foreach loop `foreach ($guild->guild_id as $id)` – musafar006 Dec 17 '18 at 06:14
  • @musafar006 i think you are getting it right..can you eloborate the complete code there.. i am at beginner stage of phpmysql thanks – johny Dec 17 '18 at 06:18
  • @johny check my answer – musafar006 Dec 17 '18 at 06:22

1 Answers1

-1

Change your code to include one more foreach loop.

<?php if ($castles): ?>
    <?php foreach ($castles as $guild): ?>
        <?php foreach ($guild->guild_id as $id): ?>
            <div class="flag">
                <img width="24" src="<?php echo $this->emblem($id) ?>">
            </div>
        <?php endforeach ?>
    <?php endforeach ?>
<?php endif ?>
musafar006
  • 931
  • 11
  • 22
  • Warning: Invalid argument supplied for foreach() – johny Dec 17 '18 at 06:25
  • can remove foreach entirely and echo each $ids manually ?? and so on,,, ... – johny Dec 17 '18 at 06:26
  • It is always useful to add the output of your script when you post your question. Please show the output of `var_dump($guild->guild_id);` – musafar006 Dec 17 '18 at 06:27
  • var_dump($guild->guild_id); of Emperium string(1) "1" string(1) "2" string(1) "3" string(1) "4" string(1) "5" – johny Dec 17 '18 at 06:37
  • is it possible to entirely remove the foreach loop. and make it manual? by manually echo $this->emblem(here guild_id of $ids=1) something like this ? – johny Dec 17 '18 at 07:11
  • I cannot answer that without having the info about the nature of your data. – musafar006 Dec 17 '18 at 09:19