3

i've this code, i'm trying to explode my date e.g "04 June 2015" to have the day and the month in other 2 variables, but i have all in a method, with an array, is it possible, my code doesn't work, it writes "Array[0]" why?

while ($i < $number) {

      $festa[$i] = mysql_result($result,$i,"festa");
      $luogo[$i] = mysql_result($result,$i,"luogo");
      $idfesta[$i] = mysql_result($result,$i,"ID");
      $data[$i] = mysql_result($result,$i,"data");  
      $nomeimg[$i] = mysql_result($result,$i,"nomeimg");

      $data[$i] = data_eng_to_it_($data[$i]);

      $data_exploded[$i] = explode(" ", $data[$i]);

      $data_exploded[$i][0] ; //day
       $data_exploded[$i][1] ; //month
        $data_exploded[$i][2] ; //year

EDIT

in this case :

while ($i < $number) {

      $festa[$i] = mysql_result($result,$i,"festa");
      $luogo[$i] = mysql_result($result,$i,"luogo");
      $idfesta[$i] = mysql_result($result,$i,"ID");
      $data[$i] = mysql_result($result,$i,"data");  
      $nomeimg[$i] = mysql_result($result,$i,"nomeimg");

      $data[$i] = data_eng_to_it_($data[$i]);

      $data_exploded = explode(" ", $data[$i]);

      $data_exploded[$i][0] ; //day
       $data_exploded[$i][1] ; //month
        $data_exploded[$i][2] ; //year

       echo" 
                              <div class=\"circleBase type3\"><div style=\"text-align:center;\">
                              <br>
                             <font size=\"+2\" style=\"margin-left:4px;\"> $data_exploded[0] </font>

it returns me "04" correctly but i can't use it because it repeats 04 for each result of my cycle.

ALL THE CODE

  <div id="container">



<?php 
//VARIABILI DI LOGIN CON FACEBOOK 

$FBID = $_GET['FBID'];
$FBUSER = $_GET['FBUSER'];
$FEMAIL = $_GET['FEMAIL'];
$DBhost = "localhost";
$DBuser = "bestparty";
$DBpass = "";
$DBName = "my_bestparty";

$count = strlen($FBID);

if ($count < 5 || !$count || $count = "undefined") {}
else {


//CHECK IF USERS EXIST
$link = mysql_connect("localhost", "bestparty", "") or die(mysql_error()); 
                                mysql_select_db("my_bestparty") or die(mysql_error()); 


                        $myquery = "SELECT * FROM Utenti WHERE Fuid = '$FBID'";

                        $resulta = mysql_query($myquery);
                        $numbera = mysql_num_rows($resulta);
                        mysql_close($link);
                        if($numbera == '0'){
                        $link1 = mysql_connect("localhost", "bestparty", "password") or die(mysql_error()); 
                                mysql_select_db("my_bestparty") or die(mysql_error()); 


                        $myquery1 = "INSERT INTO `Utenti`(`UID`, `Fuid`, `Ffname`, `Femail`, `class`) VALUES (NULL,'$FBID','$FBUSER','$FEMAIL','3')";

                        $resulta1 = mysql_query($myquery1);
                        $numbera1 = mysql_num_rows($resulta1);
                        mysql_close($link1);

                        }
                        else {

                        }
}







$table = "Database";

mysql_connect($DBhost,$DBuser,$DBpass) or die("mysql_error()");
@mysql_select_db("$DBName") or die("mysql_error()");



$sqlquery = "SELECT * FROM `Database` ORDER BY data DESC";
$result = mysql_query($sqlquery);
$number = mysql_num_rows($result);



$i = 0;

while ($i < $number) {

      $festa[$i] = mysql_result($result,$i,"festa");
      $luogo[$i] = mysql_result($result,$i,"luogo");
      $idfesta[$i] = mysql_result($result,$i,"ID");
      $data[$i] = mysql_result($result,$i,"data");  
      $nomeimg[$i] = mysql_result($result,$i,"nomeimg");

      $data[$i] = data_eng_to_it_($data[$i]);

      $data_exploded = explode(" ", $data[$i]);

      $data_exploded[$i][0] ; //day
       $data_exploded[$i][1] ; //month
        $data_exploded[$i][2] ; //year

       echo" <style>
           .circleBase {
        border-radius: 50%;
        behavior: url(PIE.htc); /* remove if you don't care about IE8 */
        }

        .type1 {
        width: 100px;
        height: 100px;
        background: yellow;
        border: 3px solid red;
        }
        .type2 {
        width: 50px;
        height: 50px;
        background: #ccc;
        border: 3px solid #000;
        }
        .type3 {
        position: absolute;
        width: 60px;
        height: 60px;
        margin-left:40%;
        margin-top: 53%;
        background: #fff;
        z-index: 1000;
        box-shadow: 1px 0px 3px rgba(0,0,0,0.24), 0 0px 3px rgba(0,0,0,0.24);
        font-size:8px;
        color: #e20a7e;
        border: solid 1px #e20a7e;
        }
            </style>
                                 <!--INIZIO DIV EVENTO-->
                              <div class=\"circleBase type3\"><div style=\"text-align:center;\">
                              <br>
                             <font size=\"+2\" style=\"margin-left:4px;\"> $data_exploded[0] </font>
                              <br>Maggio</div></div>
                                <a href=\"pagevento.html?ID=$idfesta[$i]&FBID=$FBID&FBUSER=$FBUSER\">
                                    <div style = \"display: block; margin: 0 auto; width: 95%; \">
                                        <div id=\"testihomeevento\"
                                         style=\" box-shadow: 1px 0px 3px rgba(0,0,0,0.24), 0 0px 3px rgba(0,0,0,0.24);
                                          padding:10px; display: block; margin: 0 auto; width: 85%; background-color: #fff;font-size: 0.7em;\">
                                            <font size=\"+1\" style=\"color:#e20a7e\">$festa[$i]</font> <br>
                                            $luogo[$i] <br>
                                            $data[$i] <br>
                                        </div>

                                        <div style=\"background-image:url(http://www.bestparty.altervista.org/APP/foto/$nomeimg[$i]);
                                                         background-position:center;
                                                       background-size: cover;
                                                       position: relative;
                                                       width: 100%;
                                                       height: 240px;
                                                       box-shadow: 1px 0px 3px rgba(0,0,0,0.24), 0 0px 3px rgba(0,0,0,0.24);\">

                                        </div>


                                        <div style=\" display: block; margin: 0 auto; width: 85%; background-color: #fff;
                                                    box-shadow: 1px 0px 3px rgba(0,0,0,0.24), 0 0px 3px rgba(0,0,0,0.24);\">
                                            <br>
                                            <br>
                                        </div>  

                                    </div>
                                    <br>

                                </a>

                                 <!--FINE DIV EVENTO--> 

";

$i++;
}
?>                      
                                </div>
5
  • 4
    What is $data[$i] = data_eng_to_it_($data[$i]); doing? Commented Jun 22, 2015 at 13:19
  • Please reformat your question for clarity. Commented Jun 22, 2015 at 13:21
  • What command are you using that you're saying it writes "Array[0]"? There are many different ways to print or display data in PHP, and I'm wondering if your output is simply caused by using an output function with behavior that's different than you expect. Commented Jun 22, 2015 at 13:24
  • if i do "echo $data_exploded[$i][0]" instead of print "04" it prints "Array[0]" @Tor Commented Jun 22, 2015 at 13:31
  • $data[$i] = data_eng_to_it_($data[$i]); converts from 2015-06-04 to 04 June 2015 @JonathanClark Commented Jun 22, 2015 at 13:32

1 Answer 1

3

The explode function is being used correctly, so your problem is further up.

Either $data[$i] = mysql_result($result,$i,"data"); isn't returning the expected string "2015-06-04" from the database

OR

your function $data[$i] = data_eng_to_it_($data[$i]); isn't returning the expected string "04 June 2015"

So test further up by echo / var_dump after both these two lines one at a time to make sure you're getting the strings you're expecting. In the data_eng_to_it_ function are you remembering to return $data ?


EDIT:

You are echoing $data_exploded[0] which will output 'Array'. Instead output the 2D array, so $data_exploded[0][0] will output "04". Or to see the FULL output of $data_exploded[0] use: var_dump($data_exploded[0]);

For it to work in your while loop, use $data_exploded[$i][0]

You can't however do this in a echo string "". Either concatenate it using . or assign it to a simpler variable.

  1. Simpler variable: $day = $data_exploded[$i][0] and use the day variable the same way you have
  2. Concatenate: echo "<h1>".$data_exploded[$i][0]."</h1>";
  3. Split HTML and PHP: personally I would do: ?> <h1><?php echo $data_exploded[$i][0];?></h1>
Sign up to request clarification or add additional context in comments.

7 Comments

when i echo $data[$i] after "$data[$i] = data_eng_to_it_($data[$i]);" i get correctly "04 June 2015" and when i echo $data_exploded[0] it returns me "Array"
Maybe you should post some more code. If you get "04 June 2015" at that point, echo $data_exploded[$i][0]; after the explode function should return "04"
if i echo $data_exploded[0][0] or $data_exploded[0][$i] i have as output "04[0]"
and $data_exploded[$i][0] ?
it returns me "04[0]" too
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.