Visualizzazione dei risultati da 1 a 4 su 4

Discussione: For in un generatore

  1. #1

    For in un generatore

    Sto scrivendo un generatore di tabelle html e con un mio amico abbiamo tirato fuori questo codice:
    Codice PHP:
    <?php
    $sottotitolo
    =$_POST['sottotitolo'];
    $righe=$_POST['righe'];
    $colonne=$_POST['colonne'];
    echo 
    '<table border="1">';
    if (
    $sottotitolo == 1) {
    echo 
    "<td colspan=\"$colonne\">Sottotitolo</td>";
    }
    for(
    $i=1;$i<=$righe;$i++){
        echo 
    '<tr>';
            for(
    $i=1;$i<=$colonne;$i++){
                echo 
    "<td>Colonna $i</td>";
            }
        echo 
    '</tr>';
    }
    echo 
    "</table>";
    ?>




    <form action="" method="POST">
    Righe: <input type="text" size="3" name="righe">

    Colonne: <input type="text" size="3" name="colonne">

    Sottotitolo: <select name="sottotitolo">
    <option value="1">Si</option>
    <option value="0">No</option></select>

    <input type="submit" value="Crea">
    </form>
    Il problema è che la tabella viene sempre fuori con una sola riga, anche se compiliamo il campo con 100.

    Grazie dell'aiuto

  2. #2
    Provate a non usare la stessa variabile per due for annidati

  3. #3
    Ho risolto ma dovrei far visualizzare il codice html e sto usando una classe

    classe:
    Codice PHP:
    <?php
    class sourcecode 
        

            function 
    html($str
            { 
                
    $str preg_replace("/()/""(%comment_b%)\\2(%comment_e%)"$str);  // Replace COMMENTs 
                 
                
    $tag_array preg_split("/(<.+?>)/"$str, -1PREG_SPLIT_DELIM_CAPTURE);  // Breake by TAGs to array 
                 
                
    while (list($ar_counter,$ar_value) = each($tag_array)) // walk array 
                

                    if (
    $ar_counter != 0
                    { 
                            
    //        <TAG ATTRIBUTE="VALUE" /> 
                            //replace <(%tag_b%)TAG(%span_e%) (%attribute_b%)ATTRIBUTE(%span_e%)=(%velue_b%)"VALUE"(%span_e%) /> 
                            
    $re=array("/(<+)([\/]?)(\S+)(>| [^>]*>)/""/ (\S+)( *= *)([\"']?)([^\"'>]+)([\"' \?>]?)/"); 
                            
    $replacement=array( "\\1\\2(%tag_b%)\\3(%span_e%)\\4"" (%attribute_b%)\\1(%span_e%)\\2(%value_b%)\\3\\4\\5(%span_e%)"); 
                            
    $ar_value=preg_replace($re,$replacement,$ar_value);  
                         
                            
    $ar_value=htmlspecialchars($ar_value); 
                             
                            
    // replace signs (%something%) to span HTML TAGs 
                            
    $ar_value=str_replace("(%tag_b%)""<span class=\"tag\">"$ar_value); 
                            
    $ar_value=str_replace("(%value_b%)""<span class=\"value\">"$ar_value); 
                            
    $ar_value=str_replace("(%attribute_b%)""<span class=\"attribute\">"$ar_value); 
                            
    $ar_value=str_replace("(%span_e%)""</span>"$ar_value); 
                             
                    } else  
                    { 
                        
    // replace signs (%something%) to span HTML TAGs 
                        
    $ar_value=str_replace("(%comment_b%)""<span class=\"comment\">&lt;!--"$ar_value); 
                        
    $ar_value=str_replace("(%comment_e%)""--&gt;</span>"$ar_value); 
                    } 
                     
                    
    $res.=nl2br($ar_value); 
                } 
                return 
    "<span class=\"text\">$res</span>"// return RESULT close in tags SPAN 
            

        } 
    ?>

    file php
    Codice PHP:
    <html>
    <head><title>Genera tabelle per MyBB</title>
    <style type="text/css">
    <style> 
                .code 
                { 
                    text-align:left; 
                    font-size:9px; 
                    font-family: 'Fixedsys', 'courier new', courier, fixed;s 
                } 

                .code span.text 
                { 
                    color:#000000; 
                } 
                 
                .code span.comment 
                { 
                    color:#008000; 
                } 
                 
                .code span.tag 
                { 
                    color:#0000BB; 
                } 
                 
                .code span.attribute 
                { 
                    color:#DD0000; 
                } 
                 
                .code span.value 
                { 
                    color:#800080;     
                } 
                 
                .code span.comment span 
                { 
                    color:#008000; 
                } 
                </style> 
                </head>
                <body>

    <?php
    include("hig.php");
    $hig = new sourcecode;
    $sottotitolo htmlspecialchars($_POST['sottotitolo']);
    $righe htmlspecialchars($_POST['righe']);
    $colonne htmlspecialchars($_POST['colonne']);

    function 
    table() {
    $table="<table border=\"1\">";
    if (
    $sottotitolo == 1) {
    $table.="<tr><td colspan=\"$colonne\">Sottotitolo</td></tr>";
    }
    for(
    $i=j;$j<$righe;$j++){ 
        echo 
    "<tr>";
            for(
    $i=1;$i<=$colonne;$i++){
                
    $table.="<td>Colonna $i</td>";
            }
       
    $table.="</tr>";
    }
    $table.="</table>";
    return 
    $table;

    $table table();
    echo 
    "<p class=\"code\">".$hig->html($table)."</p>"
    ?>




    <form action="" method="POST">
    Righe: <input type="text" size="3" name="righe">

    Colonne: <input type="text" size="3" name="colonne">

    Sottotitolo: <select name="sottotitolo">
    <option value="1">Si</option>
    <option value="0">No</option></select>

    <input type="submit" value="Crea">
    </form>

    </body>
    </html>
    Il problema è che mi printa sempre <table border="1"></table>

  4. #4
    Prima di tutto, invece di

    codice:
    for($i=j;$j<$righe;$j++){
    metti

    codice:
    for($j=1;$j<$righe;$j++){

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.