Repeat PHP code -


i have code know if there easier way have repeated. have 60 of these , adding more nice if me figure out loop or won't have manually add each one. each of these making link download, guessing have use variable name file name. file name 01 intro.mp3 tells download is, guessing make work have change variable name.

here code use variable names.

while ($row = mysqli_fetch_array($resultstwo))     {     extract($row);     } 

here code repeated. included 2 of them.

<?php             if ($intro ==1) {                 $strkey = createkey();                 mysqli_query($resdb,"insert downloads (downloadkey, file, expires) values ('{$strkey}', '01 intro.mp3', '".(time()+(60*60))."')");                 echo "<a href='download.php?key=$strkey'>intro</a>";             }         ?>         <?php             if ($fightingfires ==1) {                 $strkey = createkey();                 mysqli_query($resdb,"insert downloads (downloadkey, file, expires) values ('{$strkey}', '02 fighting fires.mp3', '".(time()+(60*60))."')");                 echo "<a href='download.php?key=$strkey'>fighting fires</a>";             }         ?> 

i suggest having variables within database if not included, such as:

id,caption,filename -------------------- 1,intro,01 intro.mp3 2,fighting fires,02 fighting fires.mp3 

here sql query creating table:

create table `music` (   `id` int(11) not null auto_increment,   `caption` varchar(32),   `filename` varchar(32),   primary key (`id`),   unique key `uc_filename` (`filename`) ); 

as test data can insert.

insert `music`(`caption`,`filename`) values('intro','01 intro.mp3'),('fighting fires','02 fighting fires.mp3'); 

assuming user table follows current format:

id,username,intro,fightingfires ------------------------------- 1,michael,1,null 2,dave,null,null 

i believe should altered show instead:

id,username,music_id -------------------- 1,michael,1, 2,dave,null 

the structure of table can written as:

create table `users` (  `id` int(11) not null auto_increment,  `username` varchar(32),  `music_id` int(11),  primary key (`id`),  key `music_id` (`music_id`),  constraint `users_music_id_ibfk_1` foreign key (`music_id`) references `music` (`id`) on delete cascade ); 

now when music row deleted music table, users wishing download music removed.

try query database now:

$resultstwo=$mysqli->query("select `music_id` `users` `username`='michael'"); 

change username based on user logged in, imagine have under control.

then can manage data better this:

while ($row = mysqli_fetch_array($resultstwo)) {     $strkey = createkey();     mysqli_query($resdb,"insert downloads (downloadkey, file, expires) values ('{$strkey}', '".$row['filename']."', '".(time()+(60*60))."')");     echo "<a href='download.php?key=$strkey'>".$row['caption']."</a>";     break; } 

then can better manage downloading of music adding or deleting columns table above.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -