Jump to content

Implode array not working


cutielou22
Go to solution Solved by ginerjm,

Recommended Posts

I am having trouble with an array. Don't know to much about them. I have a form on create.php where they can choose multiple items for there trade. On the create.pro.php is where I want it to put all the checked items from create.php together separated by commas into my database. (That way I can explode them later to get them separately - correct?)

 

create.php

$stmt = $mysqli->prepare("SELECT id, owner, item_id, uses_left FROM useritems WHERE owner = ? ORDER BY item_id");
$stmt->bind_param('s', $userid);
$stmt->execute();
$stmt->store_result();
$countitems = $stmt->num_rows;
$stmt->bind_result($id, $owner, $item_id, $uses_left);

ECHO <<<END
<center>
<p><a href="index.php">Home</a> | <a href="index.php?type=me">My Trades</a> | <a href="create.php">Create Trade</a> | <a href="offers.php\">My Offers</a> | <a href="search.php">Search</a></p>

<b>Note:</b> Spirits can not be traded therefore will not be shown in the items below.

<form action="create.pro.php" method="post">
   <center><table cellspacing="0" cellpadding="0" width="800">
END;

while ($stmt->fetch()){

	$stmt2 = $mysqli->prepare("SELECT name, type, value, rarity, image, description, retired FROM items WHERE itemid = ? AND type != 'spirit' AND magic_num = ?");
	$stmt2->bind_param('si', $item_id, $uses_left);
	$stmt2->execute();
	$stmt2->store_result();
	$stmt2->bind_result($name, $type, $value, $rarity, $image, $description, $retired);
	$stmt2->fetch();
	$stmt2->close();
	
	$y = $x % 5;
	if ($y == 0)
	{
		echo "<tr>";
	}
	
	if (!empty($name)){
	
	echo "
	<td>
		<img src=\"$baseurl/images/items/$image\"><br>
		<input type=\"checkbox\" name=\"use_item\" value=\"$itemid\"> <b>$name</b><br>
	</td>
	";
	
	}
	
	if ($y == 4)
	{
		echo "</tr>";
	}
	$x++;

}

$stmt->close();

echo "</table>
   <p align=\"center\">Include sP: <input type=\"text\" name=\"include_amount\" size=\"20\"></p>
	<p align=\"center\">Wishlist: <input type=\"text\" name=\"wishlist\" size=\"40\"></p>
    <input type=\"submit\" name=\"submit\" class=\"mybutton\" value=\"Create Trade\"></center>
</form>
</center>";

create.pro.php (where the form leads to - this is the page I need help with)

	$array = array($_POST['use_item']);
	$items = implode(", ", $array);
	$include_amount = $_POST['include_amount'];
	$wishlist = $_POST['wishlist'];
	
	$stmt = $mysqli->prepare("INSERT INTO trades (items, sPoints, wishlist, owner) VALUES (?, ?, ?, ?)");
	$stmt->bind_param('siss', $items, $include_amount, $wishlist, $userid);
	$stmt->execute();
	$stmt->store_result();
	$stmt->fetch();
	$stmt->close();

	die(header("Location: index.php?note=Trade+created."));

Thanks for any help!

Link to comment
Share on other sites

The code is conceptually wrong. You need to learn the basics of relational databases, namely normalization and joins.

 

Abusing strings to store comma-separated values violates the first normal form and defeats the whole purpose of having a relational database system. You lose type safety (a string can contain any garbage), you lose referential integrity (the IDs can point to nowhere), and you lose the ability to query the data in any meaningful way. You've effectively degraded your SQL database to a dumb byte store.

 

Understanding how tables are linked with joins is another important aspect. Right now, you're reinventing the square wheel by moving core SQL features into the application code, which is extremely inefficient and cumbersome.

Link to comment
Share on other sites

Thanks - I guess? You still didn't answer my question. You just gave me another problem (which I still don't completely understand - but not worried about that right now). I just want to understand arrays and how to deal with them.

 

If you guys are saying I should treat each item as a individual trade with same lot_num that is fine (and not group them together). I know how to code that. That will be easy to do - my problem is putting the array into my database (which you guys are saying is bad right?).

Link to comment
Share on other sites

You still didn't answer my question.

 

Then you haven't read or understood the replies.

 

Let's try it with a simpler message which hopefully fits into the 140-character attention span:

  • You don't understand basic SQL (like joins). As long as that's the case, it's pointless to worry about more advanced features like inserting multiple rows.
  • For the time when you do understand SQL, Sepodati has pointed you to the PHP manual which explains how form values can be stored in arrays.

The answer is right in front of you. Now you have to read it.

Link to comment
Share on other sites

  • Solution

From another reader - properly stored data is never saved as an array.  If you read up on RDBMS or 'relational databases' you will learn about how data should be stored.

 

In this case you will need to create a separate table that will have a key field related to the primary record (wherever you were planning on storing this array) and a field to store one item from this proposed array.  This will create multiple records in the new table with all of the elements you wish to save.  This may seem  an odd way when one is so enamored with the array concept, but in the long run you will see the reasons.  Of course no you have to understand how to do joins when you write a query, but that will come easily when you begin to read up on that!

  • Like 2
Link to comment
Share on other sites

Jacques,

 

I agree with you entirely. With that said, ginerjm did extrapolate and elaborate in an area that is needed. cutielou22 clearly needs some help with how to design her database properly. Then the SQL to access the joined data will become a necessity.

Link to comment
Share on other sites

I want to add one comment.

 

Please don't structure your code using the anachronistic and potentially dangerous practice of script.something.php. This can lead to exploits in some cases, where people can get your server to run code they've uploaded.

 

Modern PHP code structure utilizes directories, and files named using standard conventions.

 

These standards are described here:

 

PSR-1

http://www.php-fig.org/psr/psr-1/

and

https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-1-basic-coding-standard.md

 

PSR-2

http://www.php-fig.org/psr/psr-2/

 

Adopting these standards will also hopefully lead you to using composer for your project dependency management, and will open things up to robust autoloading support and the quick and easy adoption of high quality component libraries.

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.