Jump to content

[SOLVED] loops in php


dub_stylee

Recommended Posts

Hello again, I have another question for you PHP freaks out there :)  when I add an album to my database, it will take me to a new page where I then enter in the tracks that are on the album.  Being that there are not a fixed number of tracks on a given album, I made my default page simply look like this:

 

Track number: 1    Track name:  [      text box          ]

 

What I want to do is when I enter a track name in the text box, it will add a new line:

 

Track number: 2    Track name:    [  another text box  ]

... and so on.  I imagine that this will require a loop until I click on "Add Tracks"  button at the bottom of my page.  Here is what I have:

 

echo "Album successfully added.";
echo "<br>";
echo "<form action = 'addtracks.php' method = 'POST'>";
$track_num = 1;
echo "Track number: ".$track_num;
echo "Track name: ";
echo "<input type = 'text' name = 'track_name'>";
echo "<input type = 'submit' value = 'Add Tracks'>
      </form>\n";

 

Thanks in advance for any pointers!

 

Brian

Link to comment
Share on other sites

I need to know something before I try to answer your question, as I don't quite understand what exactly you are trying to do.

 

So you want the PHP script to automatically add the "track number #  track name: [  text box ]" the necessary amount of times after the album is submitted?

Link to comment
Share on other sites

For example, if I were to add the album "Wish You Were Here" by Pink Floyd to my database, it would generate a page that looks like:

 

Album added successfully.

 

Track number: 1    [                                  ]

 

 

... then I would proceed to enter "Shine On You Crazy Diamond Parts I-V" into the text box.  The script would then add a new line:

 

Track number: 2    [                                    ]

 

.. where I would then add "Welcome To The Machine" into the text box, which would add a new line:

 

Track number: 3  [                                    ]

 

and so on.  Until I click the "Add Tracks" button.  I'm not sure if this is very feasible, as I am very new to PHP.  After I complete this page I will then have to make my addtracks.php that will take all of the tracks submitted and add them to my database.  :)

 

Brian

Link to comment
Share on other sites

database info....

 

database name album.

 

and use a join mate.......

 

track_id int not null primary_key auto increment

album_name varchar(100) not null

 

number_id int not null primary_key auto increment

track_id int not null

track_name int not null

 

Link to comment
Share on other sites

redarrow, not sure what you mean?  I have the following tables in my database (relevant to this topic):

 

albums

album_id artist_id album_name album_label

 

tracks

album_id track_num track_name

 

My goal is to add a new album into the database, and then proceed to a new page that will allow me to add the corresponding tracks.  The tracks are stored in the database with the album_id as part of the info.  Does that make sense?? 

Brian

Link to comment
Share on other sites

redarrow, I understand that about using a join.  That is how I access the information when I am pulling it up.  My question is in regard to a PHP script that will be used for INSERT.  As far as I know JOIN is only used for SELECT queries.

 

Brian

Link to comment
Share on other sites


<?php

$sql="select 
albums.album_id,
albums.artist_id,
albums.album_name,
albums.album_label,
tracks.album_id,
tracks.track_num, 
tracks.track_name from database_name where albums.album_id='tracks.album_id'";

$res=mysql_query($sql)or dir(mysql_error());

?>

Link to comment
Share on other sites

this is the only inserts you can use as what i can see......

 

<?php
$sql="INSERT INTO albums(album_id,artist_id,album_name,album_label)VALUES('$album_id','$artist_id','$album_name','$album_label')";

$sql2="INSERT INTO tracks(album_id,track_num,track_name)VALUES('$album_id','$track_num','$track_name')";

$res1=mysql_query($sql)or die(mysql_error());

$res2=mysql_query($sql)or die(mysql_error());

?>

Link to comment
Share on other sites

somethink like this i guess......

 

<?php session_start(); 

echo "<a href='".$_SERVER['PHP_SELF']."'?album=album>Add album</a>"; 


if($_GET['album']=="album"){

?>

<form method="POST" action="<?php $_SERVER['PHP_SELF']?>">

Please Enter Album Name

<br><br>

<input type="text" name="album_name">

<br><br>

Please Enter Album Label

<br><br>

<input type="text" name="album_label">

<br><br>

<input type="submit" name="album" value="ENTER INFO">

</form>

<?php }?>



<?php
if(isset($_POST['album'])){

$album_id =mysql_real_escape_string($_POST['album_id']); 
$album_name=mysql_real_esape_string($_POST['album_name']); 
$album_label=mysql_real_esape_string($_POST['album_label']);

$_SESSION['album_id']=$album_id;


$sql="INSERT INTO albums(album_id,album_name,album_label)VALUES('$album_id','$album_name','$album_label')";


$res1=mysql_query($sql)or die(mysql_error());

if(mysql_affected_rows($res1)){


echo"

<form method='POST' action=''>

Please Enter Track Number

<br><br>

<input type='text' name='tracks_num'>

<input type='text' name='album_id' value=' ".$_SESSION['album_id']." '>

<br><br>

Please Enter Tracks Name

<br><br>

<input type='hidden' name='tracks_name' >

<br><br>

<input type='submit' name='tracks' value='SEND TRACK NAMES'>	

</form>";

if(isset($_POST['tracks'])){

$tracks_id=mysql_real_escape_string($_POST['tracks_id']);
$album_id=mysql_real_escape_string($_POST['album_id']);
$tracks_num=mysql_real_escape_string($_POST['tracks_num']);
$tracks_name=mysql_real_esape_string($_POST['tracks_name']);

$sql2="INSERT INTO tracks(tracks_id,album_id,tracks_num,track_name)VALUES('$album_id','$album_id','$tracks_num','$tracks_name')";


$res2=mysql_query($sql)or die(mysql_error());

}
}
  }

?>

Link to comment
Share on other sites

corrected


<?php session_start(); 

echo "<a href='".$_SERVER['PHP_SELF']."?album=album>Add album</a>"; 


if($_GET['album']=="album"){

?>

<form method="POST" action="<?php $_SERVER['PHP_SELF']?>">

Please Enter Album Name

<br><br>

<input type="text" name="album_name">

<br><br>

Please Enter Album Label

<br><br>

<input type="text" name="album_type">

<br><br>

<input type="submit" name="album" value="ENTER INFO">

</form>

<?php }?>



<?php

if(isset($_POST['album'])){

$album_id =mysql_real_escape_string($_POST['album_id']); 
$album_name=mysql_real_escape_string($_POST['album_name']); 
$album_label=mysql_real_escape_string($_POST['album_label']);

$_SESSION['album_id']=$album_id;


$sql="INSERT INTO albums(album_id,album_name,album_label)VALUES('$album_id','$album_name','$album_label')";


$res1=mysql_query($sql)or die(mysql_error());


if(mysql_affected_rows($res1)){

?>	


<form method="POST" action="<?php $_SERVER['PHP_SELF'] ?> ">

Please Enter Track Number

<br><br>

<input type="text" name="tracks_num">

<input type="text" name="album_id" value=" <?php echo $_SESSION['album_id']; ?>  ">

<br><br>

Please Enter Tracks Name

<br><br>

<input type='hidden' name='tracks_name'>

<br><br>

<input type='submit' name='tracks' value='SEND TRACK NAMES'>	

</form>	

<?php 

if(isset($_POST['tracks'])){

$tracks_id=mysql_real_escape_string($_POST['tracks_id']);
$album_id=mysql_real_escape_string($_POST['album_id']);
$tracks_num=mysql_real_escape_string($_POST['tracks_num']);
$tracks_name=mysql_real_escape_string($_POST['tracks_name']);

$sql2="INSERT INTO tracks(tracks_id,album_id,tracks_num,tracks_name)VALUES('$album_id','$album_id','$tracks_num','$tracks_name')";


$res2=mysql_query($sql)or die(mysql_error());

}
}
  }


?>

Link to comment
Share on other sites

my final pop at this might work not tested..........

 

first press link add album..

 

1. you enter the album name

2. you then enter album label

 

Then to another form on the same page.

 

1.you enter the tracks num  means number i hope.

2. you enter the tracks names.

 

Afther all that your shown the results from both database fields.....

 

hope it gives you an idear or a good start all the best redarrow.......

 

sorry if there any mistakes pal....

 

<?php session_start(); 

echo "<a href='".$_SERVER['PHP_SELF']."?album=album>Add album</a>"; 


if($_GET['album']=="album"){

?>

<form method="POST" action="<?php $_SERVER['PHP_SELF']?>">

Please Enter Album Name

<br><br>

<input type="text" name="album_name">

<br><br>

Please Enter Album Label

<br><br>

<input type="text" name="album_label">

<br><br>

<input type="submit" name="album" value="ENTER INFO">

</form>

<?php }?>



<?php

if(isset($_POST['album'])){

$album_id =mysql_real_escape_string($_POST['album_id']); 
$album_name=mysql_real_escape_string($_POST['album_name']); 
$album_label=mysql_real_escape_string($_POST['album_label']);

$_SESSION['album_id']=$album_id;


$sql="INSERT INTO albums(album_id,album_name,album_label)VALUES('$album_id','$album_name','$album_label')";


$res1=mysql_query($sql)or die(mysql_error());


if(mysql_affected_rows($res1)){

?>	


<form method="POST" action="<?php $_SERVER['PHP_SELF'] ?> ">

Please Enter Track Number

<br><br>

<input type="text" name="tracks_num">

<input type="text" name="album_id" value=" <?php echo $_SESSION['album_id']; ?>  ">

<br><br>

Please Enter Tracks Name

<br><br>

<input type='hidden' name='tracks_name'>

<br><br>

<input type='submit' name='tracks' value='SEND TRACK NAMES'>	

</form>	

<?php 

if(isset($_POST['tracks'])){

$tracks_id=mysql_real_escape_string($_POST['tracks_id']);
$album_id=mysql_real_escape_string($_POST['album_id']);
$tracks_num=mysql_real_escape_string($_POST['tracks_num']);
$tracks_name=mysql_real_escape_string($_POST['tracks_name']);

$sql2="INSERT INTO tracks(tracks_id,album_id,tracks_num,tracks_name)VALUES('$album_id','$album_id','$tracks_num','$tracks_name')";


$res2=mysql_query($sql)or die(mysql_error());


if(mysql_affected_rows($res2)){

$sql3="select 
albums.album_id,
albums.album_name,
albums.album_label,
tracks.tracks_id,
tracks.album_id,
tracks.tracks_num, 
tracks.tracks_name from database_name where albums.album_id='tracks.album_id'";

$res3=mysql_query($sql3)or dir(mysql_error());

while($list=mysql_fetch_assoc($res3)){

echo "Album Name : ".$list['album_name']." <br><br>
      Album Label : ".$list['$album_label']." <br><br>
      Tracks Num: ".$list['tracks_num']." <br><br>
      Tracks Name  ".$list['tracks_name']." <br><br>";
} 
  }
   }
    }
     }
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.