30. oktober 2002 - 01:36
Der er
4 kommentarer og 1 løsning
optimering af kode
Kan dette ikke optimeres/forenkles? <?php $link = array("eksperten", "sukos", "PHP", "showsource"); for($id = 0; $id <= count($link)-1; $id++) { echo"<a href='". $_SERVER['PHP_SELF'] ."?id=". $id ."'>". $link[$id] ."</a><br>\n"; } if(isset($_GET['id'])) { $hit = $_GET['id']; $ialt = count($link)-1; if($hit <= $ialt && $hit >= 1) { $nyid = $hit-1; $left = "<a href=\"$_SERVER[PHP_SELF]?id=". $nyid ."\">forrige</a>"; }else{ $left = " "; } if($hit >= 0 && $hit < $ialt) { $nyid2 = $hit+1; $right = "<a href=\"$_SERVER[PHP_SELF]?id=". $nyid2 ."\">næste</a>"; }else{ $right = " "; } ?> <p> <table width="50%" align="center" border="1"><tr> <td width="10%"><?php echo $left; ?></td> <th width="80%"><?php echo $hit+1 ."/". count($link); ?></th> <td width="10%"align="right"><?php echo $right; ?></td> </tr><tr> <th colspan="3"><h3><?php if($hit <= $id && $hit >= 0) { echo $link[$hit]; }else{ echo"Link findes IKKE!"; } ?></h3></th> </tr></table> <?php } ?> Og hvordan kan jeg lave et link som hedder f.eks. fil.php?id=sukos og så stadig få forrige og næste til at fungere?
Annonceindlæg tema
Cloud & AI
I dette særtema ser vi på, hvordan cloud og AI bliver fundamentet for virksomhedernes digitale forretning, og hvordan de nye muligheder for automatisering og forretningsværdi kan udnyttes uden at miste overblik, sikkerhed og menneskelig kontrol.
30. oktober 2002 - 02:34
#1
som her: <?php $link = array("eksperten", "sukos", "PHP", "showsource"); $ialt = count($link); $navne = array(); for($id = 0; $id < $ialt; $id++) { $navne[ $link[$i] ] = $i; echo"<a href='". $_SERVER['PHP_SELF'] ."?id=". $id ."'>". $link[$id] ."</a><br>\n"; } if ( isset($_GET['id']) ) { $hit=$_GET['id']; if ( ! is_numeric( $hit ) ) { // måske det er et kendt navn if ( isset( $navne[ $hit ] ) ) { $hit = $navne[ $hit ]; } else { $hit = -1; // ingen gyldig link } } $left = $right = " "; if ( $hit < $ialt && $hit >= 0) { $ok = true; if ( $hit > 0 ) { $left = "<a href=\"$_SERVER[PHP_SELF]?id=" .($hit-1) ."\">forrige</a>"; } if ( $hit < $ialt-1 ) { $right = "<a href=\"$_SERVER[PHP_SELF]?id=" .($hit+1) ."\">næste</a>"; } } ?> <p> <table width="50%" align="center" border="1"><tr> <td width="10%"><?php echo $left; ?></td> <th width="80%"><?php echo $hit ."/". $ialt; ?></th> <td width="10%"align="right"><?php echo $right; ?></td> </tr><tr> <th colspan="3"> <h3><?php echo ($ok)? $link[$hit] : "Link findes IKKE!"; ?></h3></th> </tr></table> <?php } ?> mvh JakobA
30. oktober 2002 - 07:11
#2
$navne[ $link[$i] ] = $i; skal forresten være $navne[ $link[$id] ] = $id; :O) Og så så'n her: <?php $link = array("eksperten", "sukos", "PHP", "showsource"); $ialt = count($link); $navne = array(); for($id = 0; $id < $ialt; $id++) { $navne[$link[$id]] = $id; echo"<a href='". $_SERVER['PHP_SELF'] ."?id=". $link[$id] ."'>". $link[$id] ."</a><br>\n"; } if ( isset($_GET['id']) ) { $hit=$_GET['id']; $visnr = $navne[ $hit ]+1; if ( ! is_numeric( $hit ) ) { // måske det er et kendt navn if ( isset( $navne[ $hit ] ) ) { $hit = $navne[ $hit ]; } else { $hit = -1; // ingen gyldig link } } $left = $right = " "; if ( $hit < $ialt && $hit >= 0) { $ok = true; $visnummer = $link[$hit]+1; if ( $hit > 0 ) { $left = "<a href=\"$_SERVER[PHP_SELF]?id=" .($hit-1) ."\">forrige</a>"; } if ( $hit < $ialt-1 ) { $right = "<a href=\"$_SERVER[PHP_SELF]?id=" .($hit+1) ."\">næste</a>"; } } ?> <p> <table width="50%" align="center" border="1"><tr> <td width="10%"><?php echo $left; ?></td> <th width="80%"><?php echo $visnr ."/". $ialt; ?></th> <td width="10%"align="right"><?php echo $right; ?></td> </tr><tr> <th colspan="3"> <h3><?php echo ($ok)? $link[$hit] : "Link findes IKKE!"; ?></h3></th> </tr></table> <?php } ?> Så kommer navnet som $id og der vises 1/4 2/4 o.s.v. Men point er dine! :O)
30. oktober 2002 - 07:17
#3
selvom jeg fatter det ikke helt! is_numeric ? Hvad godt hvad det gør men fatter det ikke lige her. Skal også gå nu, så må kikke på det senere!
30. oktober 2002 - 14:38
#4
is_numeric checker bare om variablen er et talt
30. oktober 2002 - 15:34
#5
is_numeric for at skelne imellem ?id=1 og ?id=sukos det er først bagefter du kan stole på at $hit er et tal, så vent med at sætte $visnr til bagefter. <?php function self_link( $id_param, $tekst ) { return "<a href='" .$_SERVER['PHP_SELF'] ."?id=$id_param'>$tekst</a>"; }; //endfunction self_link -> string $link = array("eksperten", "sukos", "PHP", "showsource"); $ialt = count($link); $navne = array(); for($id = 0; $id < $ialt; $id++) { $navne[$link[$id]] = $id; echo self_link( $link[$id], $link[$id] ) ."<br>\n"; } if ( isset($_GET['id']) ) { $hit = $_GET['id']; if ( ! is_numeric( $hit ) ) { $hit = ( isset( $navne[ $hit ] ) ) ? $navne[ $hit ] : -1; } } else { $hit = -1; // ingen gyldig parameter } $visnr = $hit+1; // både $hit og $visnr tal-variable $left = $right = " "; if ( $hit > 0 ) $left = self_link( min($ialt-1,$hit-1), "forrige" ); if ( $hit < $ialt-1 ) $right = self_link( max(0,$hit+1), "næste" ); ?> <p> <table width="50%" align="center" border="1"><tr> <td width="10%"><?php echo $left; ?></td> <th width="80%"><?php echo $visnr ."/". $ialt; ?></th> <td width="10%"align="right"><?php echo $right; ?></td> </tr><tr> <th colspan="3"> <h3><?php echo (isset($link[$hit]))? $link[$hit] : "Link findes IKKE!"; ?></h3></th> </tr></table> tak for pts.
Vi tilbyder markedets bedste kurser inden for webudvikling