17. november 2007 - 15:36
Der er
3 kommentarer og 1 løsning
Som 1 funktion
Hej hej, hvordan "gemmer" jeg nedenstående som en funktion, så jeg kan kalde den recursive kode der afvikles? --------------------------- <select name="news_archive_id" style="width: 206px" class="input"> <?php $con = mysql_connect( ****, ****, **** ); mysql_select_db( ****, $con ); $results = array(); function news_archives_list( $parent, $indent = 0 ) { global $results; $sql = "SELECT * FROM tbl_news_archive WHERE news_archive_parent_id = $parent"; //echo $sql."<br />"; $res = mysql_query( $sql ); if ( mysql_num_rows( $res ) > 0 ) { while ( $row = mysql_fetch_assoc( $res ) ) { $results[] = array("news_archive_name" => $row['news_archive_name'], "news_archive_id" => $row['news_archive_id'], "news_archive_parent_id" => $row['news_archive_parent_id'], "indent" => $indent); news_archives_list( $row['news_archive_id'], $indent + 1 ); } mysql_free_result( $res ); } } news_archives_list( 0 ); $n = count( $results ); foreach ( $results as $row ) { $line.= ""; $indent = str_repeat( "'-", $row['indent'] ); if ($row['indent'] == 0) { $line.= "<option value=\"".$row[news_archive_id]."\">".$row[news_archive_name]."</option>\n"; } else { $line.= "<option value=\"".$row[news_archive_id]."\">".$indent." ".$row[news_archive_name]."</option>\n"; } } echo $line; ?> </select>
Annonceindlæg fra Computerworld
AI-agenterne kommer vrimlende
Virksomheder er på vej fra store sprogmodeller, der svarer på spørgsmål, til AI-agenter, der kan udføre opgaver på egen hånd. Det gør teknologien mere nyttig – og langt mere risikabel.
17. november 2007 - 21:13
#1
Noget i denne stil? $con = mysql_connect( ****, ****, **** ); mysql_select_db( ****, $con ); function news_archives_list( $parent = 0, $indent = 0, $innerRecursion = false ) { global $results; if ($innerRecursion) { $sql = "SELECT * FROM tbl_news_archive WHERE news_archive_parent_id = $parent"; // echo $sql . "<br />"; $res = mysql_query( $sql ); if ( mysql_num_rows( $res ) > 0 ) { while ( $row = mysql_fetch_assoc( $res ) ) { $results[] = array( "news_archive_name" => $row['news_archive_name'], "news_archive_id" => $row['news_archive_id'], "news_archive_parent_id" => $row['news_archive_parent_id'], "indent" => $indent ); news_archives_list( $row['news_archive_id'], $indent + 1, $innerRecursion ); } } mysql_free_result( $res ); } else { echo '<select name="news_archive_id" style="width: 206px" class="input">'; $results = array(); news_archives_list( 0, 0, true ); $n = count( $results ); foreach ( $results as $row ) { $indent = str_repeat( "'-", $row['indent'] ); if ($row['indent'] == 0) { $line .= "<option value=\"" . $row[news_archive_id] . "\">" . $row[news_archive_name] . "</option>\n"; } else { $line .= "<option value=\"" . $row[news_archive_id] . "\">" . $indent . " " . $row[news_archive_name] . "</option>\n"; } } echo $line; echo '</select>'; }