Array - Teile

Sasser

Erfahrenes Mitglied
Hallo Leute, ich habe einen Codeschnipsel, welcher alle Informationen eines Artikels in einer Tabelle ausgibt. Für jeden Artikel wird eine neue <TR> genutzt.

Ich möchte nun aber an die einzelnen Informationen des Artikels ran, die Schleife für jeden Artikel soll bestehen bleiben. Leider bekomme ich das irgendwie nicht hin!

Zur Zeit wird einfach jede Information zu einem Artikel ausgegeben in einer Schleife!

PHP:
<?php
function search($array, $recursive = false, $return = false, $null = '&nbsp;')
{
        if (empty($array) || !is_array($array)) {
                return false;
    }

    if (!isset($array[0]) || !is_array($array[0])) {
        $array = array($array);
    }

    $table = "<table border='0'>\n";

        foreach ($array as $row) {
        $table .= "\t<tr>" ;
                foreach ($row as $cell) {
                        $table .= "<td style='border-width:1px;border-style:solid;border-color:#008000;' bgcolor='#90EE90'>";

            if (is_object($cell)) { $cell = (array) $cell; }

            if ($recursive === true && is_array($cell) && !empty($cell)) {
                $table .= "\n" . search($cell, true, true) . "\n";
            } else {
                $table .= (strlen($cell) > 0) ?
                    htmlspecialchars((string) $cell) :
                    $null;
            }

            $table .= '</td>';
                }

        $table .= "</tr>\n";
        }

    $table .= '</table>';

    if ($return === false) {
        echo $table;
    } else {
        return $table;
    }
}

?>
 
Also nur dass Ihr das richtig versteht;

Zur Zeit wird einfach solange eine Spalte gemacht solange das Array noch Inhalt enthält, aber ich möchte ja gern zum Beispiel den 2. Teil in einen Link verwenden etc.

Wie kann ich das realisieren, dass ich an die Einzelnen Teile ran komme ohne foreach das auszugeben;

PHP:
foreach ($row as $cell) {

                        $table .= "<td>";


            if ($recursive === true && is_array($cell) && !empty($cell)) {
                $table .= search($cell, true, true);
            } else {
                $table .= (strlen($cell) > 0) ?
                    htmlspecialchars((string) $cell) :
                    $null;
            }

            $table .= '</td>';
                }

Danke für eure Hilfe :)
 
Hi,

ich weiß nicht wie deine Arrays aussehen, aber ich gebe dir einfach mal ein allgemeines Beispiel.

Also wenn ich mir Daten mit einer foreach() ausgeben lasse, mache ich das in etwa so:
Code:
foreach($row as $data){
echo "<tr>";
echo "<td>" . $data['name'] . "</td><td>" . $data['beschreibung'] . "</td>"; usw...
echo "</tr>";
}

Sowas in der Art müsste in deiner search() Funktion auch funktionieren.

Vielleicht hilft der Ansatz ja schon.
 
Ja, aber das hilft mir leider nicht weiter, da das ja bisher schon so ist!

Also noch mal genauer erklärt;

Bisher wird durch die folgende Schleife:

PHP:
<?

function search($array, $recursive = false, $return = false, $null = '&nbsp;')
{
        if (empty($array) || !is_array($array)) {
                return false;
    }

    if (!isset($array[0]) || !is_array($array[0])) {
        $array = array($array);
    }

    $table = "<table border='1'>";

        foreach ($array as $row) {
        $table .= "<tr>" ;
                foreach ($row as $cell) {

                        $table .= "<td>";


            if ($recursive === true && is_array($cell) && !empty($cell)) {
                $table .= search($cell, true, true);
            } else {
                $table .= (strlen($cell) > 0) ?
                    htmlspecialchars((string) $cell) :
                    $null;
            }

            $table .= '</td>';
                }

        $table .= "</tr>";
        }

    $table .= '</table>';

    if ($return === false) {
        echo $table;
    } else {
        return $table;
    }
}

?>

folgendes ausgegeben:

Demo

Aber wie komme ich an die verschiedenen Arrayteile ran, ohne dass einfach per Schleife einfach solange ausgegeben wird bis nichts mehr vorhanden ist?

Wie komme ich z.B. an "ShopURL" ran? Ich nehme an, die Arrayteile sind benannt wie im Bsp. zu sehen ist!

Bitte um Hilfe! :(
 
Sorry, so sieht der originale Quelltext aus:

PHP:
<?php

function search($array, $recursive = false, $return = false, $null = '&nbsp;')
{
    // Sanity check
        if (empty($array) || !is_array($array)) {
                return false;
    }

    if (!isset($array[0]) || !is_array($array[0])) {
        $array = array($array);
    }

    // Start the table
    $table = "<table>\n";

    // The header
    $table .= "\t<tr>";
    // Take the keys from the first row as the headings
        foreach (array_keys($array[0]) as $heading) {
                $table .= '<th>' . $heading . '</th>';
        }
    $table .= "</tr>\n";

    // The body
        foreach ($array as $row) {
        $table .= "\t<tr>" ;
                foreach ($row as $cell) {
                        $table .= '<td>';

            // Cast objects
            if (is_object($cell)) { $cell = (array) $cell; }

            if ($recursive === true && is_array($cell) && !empty($cell)) {
                // Recursive mode
                $table .= "\n" . search($cell, true, true) . "\n";
            } else {
                $table .= (strlen($cell) > 0) ?
                    htmlspecialchars((string) $cell) :
                    $null;
            }

            $table .= '</td>';
                }

        $table .= "</tr>\n";
        }

    // End the table
    $table .= '</table>';

    // Method of output
    if ($return === false) {
        echo $table;
    } else {
        return $table;
    }
}

?>

Wie komme ich an die einzelnen ran? :confused:
 
Aber wie komme ich an die verschiedenen Arrayteile ran, ohne dass einfach per Schleife einfach solange ausgegeben wird bis nichts mehr vorhanden ist?
du musst in der foreach Schleife sagen welche Teile du ausgeben möchtest.
Wie komme ich z.B. an "ShopURL" ran? Ich nehme an, die Arrayteile sind benannt wie im Bsp. zu sehen ist!
das ist richtig, da der Kommentar // Take the keys from the first row as the headings schon besagt das damit der Array-Schlüssel als Überschrift angezeigt wird.

da daraufhin aber einfach alle Teile durchlaufen werden, musst du hier die foreach() und die ausgabe anpassen, ich schieß einfach mal ins blaue:
PHP:
foreach ($array as $heading) {
       $table .= '<th>' . $heading['ShopURL'] . '</th>';
       $table .= '<th>' . $heading['ShopName'] . '</th>';
       $table .= '<th>' . $heading['ImageURL'] . '</th>';
}
die ausgabe der Zeilen muss natürlich entsprechend geändert werden.
PHP:
// The body
foreach ($array as $row) {
        $table .= "\t<tr><td>" .$row['ShopURL'] ."</td>";
        $table .= "\t<tr><td>" .$row['ShopName'] ."</td>";
        $table .= "\t<tr><td>" .$row['ImageURL'] ."</td></tr>";
}
so in etwa.... ;)
 
Danke dir schonmal! :)

Aber leider wird mir nichts ausgegeben!?

PHP:
<?php

function search($array, $recursive = false, $return = false, $null = '&nbsp;')
{
    // Sanity check
        if (empty($array) || !is_array($array)) {
                return false;
    }

    if (!isset($array[0]) || !is_array($array[0])) {
        $array = array($array);
    }

    // Start the table
    $table = "<table border='1'>";

    // The body
        foreach ($array as $row) {
        $table .= "<tr>" ;
                
				
	    $table .= "<td>" .$row['ShopURL'] ."</td>";
        $table .= "<td>" .$row['ShopName'] ."</td>";
        $table .= "<td>" .$row['ImageURL'] ."</td>";
    				

        $table .= "</tr>";
        }

    // End the table
    $table .= '</table>';

    // Method of output
    if ($return === false) {
        echo $table;
    } else {
        return $table;
    }
}

?>
 
Zuletzt bearbeitet:
dann evtl. so?
PHP:
foreach ($array[0] as $row) {
        $table .= "<td>" .$row['ShopURL'] ."</td>";
        $table .= "<td>" .$row['ShopName'] ."</td>";
        $table .= "<td>" .$row['ImageURL'] ."</td>";
    }

ansonsten mal mit var_dump() arbeiten

PHP:
foreach ($array[0] as $row) {
        $table .= "<td>" .var_dump($row) ."</td>";
    }

wenn NULL zurück kommt, ist irgendwas total falsch :D
 
Zuletzt bearbeitet:
So mein Quelltext sieht nun so aus:

PHP:
<?php

function search($array)
{
    $table = "<table border='1'>";
    foreach($array as $row){
	$table .= '<tr>';
    $table .= "<td>".$row['ShopInformation']['Logo50']['ImageURL']."</td>";

    $table .= '</tr>';
    }		
    $table .= '</table>';
return $table;
}

?>
 
Zuletzt bearbeitet:
Zurück