Supportnet Computer
Planet of Tech

Supportnet / Forum / Skripte(PHP,ASP,Perl...)

Probleme mir for-schleife und array





Frage

Hallo zusammen, vielleicht kann mir ja einer bei diesem Problem hier helfen? Ich habe nen Array und will je nachdem wieviele Elemente in dem Array sind so oft eine Tabellenzeile ausgeben, bis das Ende des Arrays erreicht ist. Hier mal der Code: [code] echo '<table border="1" cellpadding="0" cellspacing="0">'; if ($anzahl>1) { for ($i=1; $i<$anzahl; $i++); { echo ' <tr> <td width="'.$abstand.'"> </td> <td ><img src="'.$subMenu_inakt[$i].'" width="99" height="20"></td> </tr>'; } echo '</table>'; } [/code] Also die Variablen sind ok, aber er gibt immer nur das letzte Element eines Arrays aus und folglich immer nur eine Zeile. Wo steckt der Fehler??? Falls es wichtig ist, hier mal Info zu $subMenu_inakt: [code] ... $menu_de_inakt_Aktuelles=array ("1" => "img/Button_Aktuelles_inaktiv.gif", "2" => "img/aktuelleArtikel_inaktiv.gif" , "3" => "img/Mission_inaktiv.gif", "4" => "img/News_inaktiv.gif", "5" => "img/Newsletter_inaktiv.gif" ); ... $subMenu_inakt=$menu_de_inakt_Aktuelles; ... [/code] Danke!

Antwort 1 von naja

zeig mal wie du die var anzahl ermittelst.

Antwort 2 von Ines123

So mach ichs:

$anzahl=count($subMenu_akt);

Wird auch in der richtigen größe ausgegeben (mit echo). Hab daraus mal ne while Schleife gemacht und es fuktioniert, wollte aber gern ne for-schleife :-(.



Antwort 3 von Nicolas

Hallo Ines123,
du hast eine leere for Schleife gemacht:
for ($i=1; $i<$anzahl; $i++); <-- bei dem ; ist die zuende.


echo '<table border="1" cellpadding="0" cellspacing="0">';

if ($anzahl>1)
{
  for ($i=1; $i<$anzahl; $i++)
  {
    printf("\n<tr>\n<td width=\"%s\"> </td>\n
    <td ><img src=\"%s\" width=\"99\" height=\"20\"></td>\n</tr>",
    $abstand,
    $subMenu_inakt[$i])
  }
}
echo '</table>'; 


Antwort 4 von Nicolas

oje jetzt hab ich dafür das ; hinter dem printf vergessen:

echo '<table border="1" cellpadding="0" cellspacing="0">';

if ($anzahl>1)
{
  for ($i=1; $i<$anzahl; $i++)
  {
    printf("\n<tr>\n<td width=\"%s\"> </td>\n
    <td ><img src=\"%s\" width=\"99\" height=\"20\"></td>\n</tr>",
    $abstand,
    $subMenu_inakt[$i]);
  }
}
echo '</table>'; 

das "echo '</table>';" muss auf jeden fall ausserhalb der if Abfrage stehen, bzw das "echo '<table..." innerhalb, du musst drauf achten das durch nichterfüllen der if bedingung nicht ein <table> oder </table> ohne sein jeweiliges Gegenstück übrig bleibt.

Antwort 5 von Ines123

Oh man, da muss ich blind gewesen sein! Nur wegen dem Semikolon??? Das hab ich gar nicht gemerkt, muss aus versehen da hingerutscht sein :-)))).

Danke Dir! Vier Augen sehen doch mehr als zwei :-)

Ines123

Ich möchte kostenlos eine Frage an die Mitglieder stellen:


Ähnliche Themen:


Suche in allen vorhandenen Beiträgen: