Jump to content

Making an array from a loop


rvinikof

Recommended Posts

Using AJAX, I have a drop down that lists a number of books. Once a number is picked, where q is the number picked, it does this:

 

for($i=0; $i < $q; $i++) 
		{
			echo '<b>Book author:</b> <input type = "text" name = "bookauthor"/><br/><br/>';
			echo '<b>Book title:</b> <input type = "text" name = "booktitle"/><br/><br/>';
			echo '<b>Book edition:</b> <input type = "text" name = "edition"/><br/><br/>';
			echo '<b>ISBN:</b> <input type = "text" name = "isbn"/><br/><br/>';
			echo '<b>Required or Optional?</b> <select name = "reqoropt">';
			echo '<option value = "required">Required</option><option value = "optional">Optional</option></select><br/><br/>';
			echo "<hr/>";
		}

 

Once this is submitted, I have requestIDs and a courseID that is inserted into a bookrequest table q number of times. That part works. I need to now insert the author, title, ect. into this table as a new line so that each author, title ect. gets a different requestID.

 

Right now, if q is 2, my table will look like this:

 

requestID courseID  author title ect.

1                12

2                12

 

Instead of:

 

requestID course ID  author    title ect.

1                12        author1  title1

2                12        author2  title2

 

I think I need to make the author, title, ect. into an array and then insert it, but everything I've tried isn't really working.

Link to comment
Share on other sites

You can make the names of inputs into an array. For example, you might have:

 

<input type = "text" value="names[]" /> <br />
<input type = "text" value="names[]" /> <br />
<input type = "text" value="names[]" /> <br />
<input type = "text" value="names[]" /> <br />

 

Your php might then contain:

 

<?php
foreach($_POST['names'] as $value){
echo $value.'<br />';//would echo each of the 4 names that were entered.
}
?>

 

Obviously just an example, but you should see how you can apply it to your code.

Link to comment
Share on other sites

You'll need to have your insert query inside the while/for loop.

 

<?php
for($x=0;$x<=count($yourarray);$x++){
mysql_query("INSERT INTO `yourtable` VALUES ('$somearray[$x]','$anotherarray[$x]')");
}
?>

 

Again, just an example. Not sure where you're at so cant give anything more specific.

Link to comment
Share on other sites

My insert statement looks like this:

 

for ($i = 0; $i < $Numofbooks; $i++)
		{ $sqlinsertlogin = "INSERT INTO bookrequest (requestID, loginID, courseID, author) VALUES ('', '".$loginid[0]."', '".$crnforclass[0]."', '".$Author"')";
			$insertlogin = mysql_query($sqlinsertlogin);
			if (!$insertlogin)
				{ $insertloginerr = mysql_error();
				   echo $insertloginerr;
			           exit(); }
				 }		

 

If I put $Author[$i], then I get the first letter of the the very last value in one row and the second letter of the very last value in the next row and so on. If I just leave $Author, then it puts the last value in every row.

Link to comment
Share on other sites

foreach($_POST["bookauthor"] as $Author)

{ echo $Author.'<br/>'; }

 

I have it pulled in from the form and used in a foreach like you suggested. I mean, it is pulled from the form as a string I guess. Where am I supposed to change it? Or do I have to define it differently in my database?

Link to comment
Share on other sites

Well you dont need that foreach, since you're not actually wanting to echo the values. Try something like:

 

<?php
$Author = $_POST['bookauthor'];
for ($i = 0; $i < $Numofbooks; $i++)
		{ $sqlinsertlogin = "INSERT INTO bookrequest (requestID, loginID, courseID, author) VALUES ('', '".$loginid[0]."', '".$crnforclass[0]."', '".$Author"')";
			$insertlogin = mysql_query($sqlinsertlogin);
			if (!$insertlogin)
				{ $insertloginerr = mysql_error();
				   echo $insertloginerr;
			           exit(); }
				}
?>	

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.