php array equals the same thing in every row

Discussion in 'PHP' started by xbat, Jan 2, 2014.

  1. #1
    I have something that looks like this.



    
    $t='yes';
    for($i=0;$i<count($stuff);$i++) {
    echo $t=$make;
      echo $tt=$stuffl[$i];
    }
    PHP:

    What I am trying to do make the same column equal the same variable. But when I try the code above all I get is array to show up in the mysql database instead of what equals $t. Any pointers or suggestions would be appreciated.
     
    Solved! View solution.
    xbat, Jan 2, 2014 IP
  2. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #2
    Can you post the other code and show what's inside the array?
     
    HuggyStudios, Jan 2, 2014 IP
  3. xbat

    xbat Well-Known Member

    Messages:
    326
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    105
    #3
    $stuff= a bunch of numbers I am getting that to fill in right. Its just repeating the of the $t inside the for {} I tried adding the [$i] but then I get array that shows up inside every column. Instead of the word yes
     
    $t='yes';
    $stuff=numbers etc...
    $make='yes';
    for($i=0;$i<count($stuff);$i++) {
    echo $t=$make;
      echo $tt=$stuff[$i];
    
    
    
    $q = "INSERT INTO something (`hi`, `stuff`) VALUES ('$t', '$tt')";
        $r = mysqli_query ($dbconnect, $q) or die ("Update query failed : " . mysql_error());
    }
    
    
    PHP:
     
    Last edited: Jan 2, 2014
    xbat, Jan 2, 2014 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    1. Is count($stuff) not supposed to be count($stuffl)?
    2. Your weird variable/table/cell naming makes debugging hard as we don't know what these things are supposed to represent.
    3. Again, the query is outside the loop, so it only runs once, using the last values in your array.
    4. Please post the contents of $stuff. Is it a multidimensional array?
    5. What does "echo $tt;" in the loop output?
     
    nico_swd, Jan 2, 2014 IP
  5. xbat

    xbat Well-Known Member

    Messages:
    326
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    105
    #5

    Thanks.. I did a quick markup. I have corrected the example code. My ultimate goal is to get the mysql table to look like this
    hi stuff
    yes 5
    yes 8
    yes 9
    yes 10
    yes 11
    yes 12

    Now with the code above I am getting

    array 5
    array 8
    array 9
    array 10
    array 11
    array 12

    All I am trying to do is get my vaibale $t='yes'; into my column.. But I have to repeat the variable thats what I am having trouble with. I can get it to go into the first row like this.
    yes 5
    array 8
    array 9
    array 10
    array 11
    array 12
    array 8
    array 9
    array 10
    array 11
    array 12
     
    xbat, Jan 2, 2014 IP
  6. #6
    
    $t='yes';
    $stuff=numbers etc...
    $make='yes';
    for($i=0;$i<count($stuff);$i++) {
    $t=$make; // you might as well not bother with this one!
     $tt=$stuff[$i];
    
    
    
    $q = "INSERT INTO something (`hi`, `stuff`) VALUES ('$t', '$tt')";
       $r = mysqli_query ($dbconnect, $q) or die ("Update query failed : " . mysql_error());
    }
    
    PHP:
     
    HuggyStudios, Jan 2, 2014 IP
  7. xbat

    xbat Well-Known Member

    Messages:
    326
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    105
    #7
    I get array that shows in every row if I use that.


     
    xbat, Jan 2, 2014 IP
  8. xbat

    xbat Well-Known Member

    Messages:
    326
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    105
    #8
    I found out how to do this a different way, however huggystudios I did use it with part of your answer. What I did was just run another foreach inside the for() Thank you very much for everyone's help.

    $t='yes';
    $stuff=numbers etc...
    $make='yes';
    for($i=0;$i<count($stuff);$i++) {
    
      foreach(') as $make) {
    echo $t=$make;
    
    
    }
    
      echo $tt=$stuff[$i];
    $q = "INSERT INTO something (`hi`, `stuff`) VALUES ('$t', '$tt')";
        $r = mysqli_query ($dbconnect, $q) or die ("Update query failed : " . mysql_error());
    }
    PHP:
     
    xbat, Jan 2, 2014 IP
  9. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #9
    I'm sorry, but your code still doesn't make any sense. First of all, that you get an array into the db, makes no sense - nowhere are you assigning an array. You're having two variables doing the same thing - $t = 'yes'; means $t is 'yes', and $make = 'yes'; means $make is 'yes'. You're doing a for-loop on $stuff, and since you're declaring both $t and $make outside the loop, they should stay the same regardless - hence, doing the for-loop without the foreach should yield the result you're after.
    Hence, the following code should work just fine:
    
    $t = 'yes';
    $stuff = array(1,5,9,17,29); //example array
    for ($i = 0; $i < count($stuff); $i++) {
      $tt = $stuff[$i];
      $q = "INSERT INTO something (`hi`, `stuff`) VALUES ('$t', '$tt')";
      mysqli_query($dbconnect, $q) or die ("Update query failed : " . mysql_error());
    }
    
    PHP:
    That should do exactly what you're trying to accomplish, given the examples you provided.
     
    PoPSiCLe, Jan 2, 2014 IP
  10. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #10
    This is also a stellar example of when prepared queries would be VASTLY superior, since you can prepare once and then bind/execute many times. I'd also ditch the stupid array indexing for loop for a foreach since you aren't actually doing anything with the index... and lose that $t variable since that doesn't actually seem to be doing anything.

    * note * the following uses php 5.4+ "javascript style" array declaration. If it throws an error, update your PHP to something modern... I'm also assuming $dbconnect is a mysqli object.

    $stuff = [1,5,9,17,29]; //example array
    
    $statement = $dbconnect->prepare('
    	INSERT INTO something (
    		hi, stuff
    	) VALUES (
    		?, ?
    	)
    ');
    
    foreach ($stuff as $value) {
    	$statement->bindParam('ss', 'yes', $value);
    	$statement->execute();
    }
    Code (markup):
    A LOT simpler... and one of the entire reasons we're supposed to be using mysqli or PDO instead of the old mysql_ functions... and if you turn off prepare emulation, it'll run faster since the query is actually sent to the engine, then for each iteration you're only sending the new data.
     
    deathshadow, Jan 3, 2014 IP
  11. xbat

    xbat Well-Known Member

    Messages:
    326
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    105
    #11
    Thank you for extras :)
     
    xbat, Jan 10, 2014 IP