Hi,
I'm trying to add this multidimensional array to my database. I've got a multidimensional array called $items. For this example, I have two items in $items, just to make it simple.
As you'll notice, I've got two variables called $player and $id. When I set these to for example $player = "test"; and $id = "blabla"; it adds two rows to the database, as it should. But when I use the code below, it only adds one row. Why does it only recognize one item, instead of both? I've tried to echo $player, and it shows the correct item. Both of them. So, apparently they're both there, but it just won't add both to the database.
$j = 0;
while($j < count($items)){
$player = $items[$j]['name'];
$id = $items[$j]['id'];
$status = "none";
mysql_query("INSERT INTO css1_css (name,id,status) VALUES('$player','$id','$status')");
$j++;
}
This is weird... to me
Moderator: General Moderators
Re: This is weird... to me
Can you show me exactly how your array look like?
Just another example how I would do it:
Just another example how I would do it:
Code: Select all
foreach($items as $item) {
$status = "none";
mysql_query("INSERT INTO css1_css (name,id,status) VALUES('{$item['player']}','{$item['id']}','$status')");
}
-
InfinityRogue
- Forum Newbie
- Posts: 3
- Joined: Tue Aug 25, 2009 2:25 am
Re: This is weird... to me
The whole thing looks like this:
<?php
require_once ('mysql_connect.php');
$file = file_get_contents('allinfo.txt');
preg_match_all('/ID:\sID_[0-9]+:[0-9]+:[0-9]+/', $file, $ids);
preg_match_all('/Name:\s[^.]+\s\|\sID\:/', $file, $names);
$i = 0;
while($i < count($ids[0])) {
$name = str_replace(' | ID:', '', $names[0][$i]);
$name = str_replace('Name: ', '', $name);
$id = str_replace('ID: ', '', $ids[0][$i]);
$auth = explode(':', $id);
$comid = $auth[2] *2 + 7960265728 + $auth[1];
$info[$i] = $comid;
$vars[$i]['name'] = $name;
$vars[$i]['id'] = $comid;
$i++;
}
$info = array_unique($info);
$count = 0;
foreach ($info as $i => $value) {
$items[$count]['name'] = $vars[$i]['name'];
$items[$count]['id'] = $vars[$i]['id'];
$count++;
}
$j = 0;
while($j < count($items)){
$player = $items[$j]['name'];
$id = $items[$j]['id'];
$status = "none";
mysql_query("INSERT INTO css1_css (name,id,status) VALUES('$player','$id','$status')");
$j++;
}
?>
<?php
require_once ('mysql_connect.php');
$file = file_get_contents('allinfo.txt');
preg_match_all('/ID:\sID_[0-9]+:[0-9]+:[0-9]+/', $file, $ids);
preg_match_all('/Name:\s[^.]+\s\|\sID\:/', $file, $names);
$i = 0;
while($i < count($ids[0])) {
$name = str_replace(' | ID:', '', $names[0][$i]);
$name = str_replace('Name: ', '', $name);
$id = str_replace('ID: ', '', $ids[0][$i]);
$auth = explode(':', $id);
$comid = $auth[2] *2 + 7960265728 + $auth[1];
$info[$i] = $comid;
$vars[$i]['name'] = $name;
$vars[$i]['id'] = $comid;
$i++;
}
$info = array_unique($info);
$count = 0;
foreach ($info as $i => $value) {
$items[$count]['name'] = $vars[$i]['name'];
$items[$count]['id'] = $vars[$i]['id'];
$count++;
}
$j = 0;
while($j < count($items)){
$player = $items[$j]['name'];
$id = $items[$j]['id'];
$status = "none";
mysql_query("INSERT INTO css1_css (name,id,status) VALUES('$player','$id','$status')");
$j++;
}
?>
Re: This is weird... to me
It's a bit messy but try with some easy debugging first. One section at a time.
In your while loop:
See if your array is correct, if not start working your way through the code to see where it fails.
In your while loop:
Code: Select all
while($j < count($items)){
$player = $items[$j]['name'];
$id = $items[$j]['id'];
$status = "none";
echo "<pre>";
print_r($items);
//mysql_query("INSERT INTO css1_css (name,id,status) VALUES('$player','$id','$status')");
$j++;
}-
InfinityRogue
- Forum Newbie
- Posts: 3
- Joined: Tue Aug 25, 2009 2:25 am
Re: This is weird... to me
It's definitely happening in this part here:
mysql_query("INSERT INTO css1_css (name,id,status) VALUES('$player','$id','$status')");
I tried putting in what you said with the {} around the values:
mysql_query("INSERT INTO css1_css (name,id,status) VALUES('{$item['player']}','{$item['id']}','$status')");
And at least now it's making two rows in the database, but they're null. I've tried printing/echoing out the values, and they are correct, so it's definitely happening when I make the query to the database. I need to figure out how to put the right values in there.
mysql_query("INSERT INTO css1_css (name,id,status) VALUES('$player','$id','$status')");
I tried putting in what you said with the {} around the values:
mysql_query("INSERT INTO css1_css (name,id,status) VALUES('{$item['player']}','{$item['id']}','$status')");
And at least now it's making two rows in the database, but they're null. I've tried printing/echoing out the values, and they are correct, so it's definitely happening when I make the query to the database. I need to figure out how to put the right values in there.