0

This code is giving error. I have gone through previous questions. I have used gettype() and it shows that my data is integer. I have tried to convert it to string by using strval() but it didnt work. I saw this but in dont know how this can be implemented with my code.

sugested similar questions talk about undefined variables. My problem not undefine but that its value type. variables are defined. It is stored in db as string but when i check its variable type using gettype(), it is an integer. and it seems foreach() doesnt work with integer.

If anyone can help please. i am still in the learning stage.

foreach($schedule_data as $schedule_row)
{
    $end_time = strtotime($schedule_row["agent_schedule_end_time"] . ':00');
    $start_time = strtotime($schedule_row["agent_schedule_start_time"] . ':00');
    $total_agent_available_minute = ($end_time - $start_time) / 60;
    $average_inspection_time = $schedule_data["average_inspection_time"];
}

i wrote query

SELECT * FROM agent_schedule_table 
WHERE agent_schedule_id = '$agent_schedule_id'

data stored in $schedule_data var_dump ()

array(8) { 
    ["agent_schedule_id"]=> int(18) 
    ["agent_id"]=> string(7) "S1O0NWE" 
    ["agent_schedule_date"]=> string(10) "2022-08-18" 
    ["agent_schedule_day"]=> string(8) "Thursday" 
    ["agent_schedule_start_time"]=> string(5) "08:35" 
    ["agent_schedule_end_time"]=> string(5) "22:35" 
    ["average_inspection_time"]=> int(60) 
    ["agent_schedule_status"]=> string(6) "Active" 
}

foreach()

var_dump($end_time); = int(1660595700)

//Agent create appointment for himself Agent add through a form that has Date: ..., starttime: ...., averagetime: ...., endtime:.....

i wrote query to select from appointemnt the agent appointment time.

i used while() for the stmt-> excute

Problem 1: while loop make it repeat same date and time so many times. I tried using foreach() or for(), it is not working. I dont know if am using it wrongly or just that it wont work.

Problem 2: what i want to achieve is that the table should; for every date, role1 start time, role2 start time + average time, role 3 role2time + average time. so the output will be like peter 22/08 4:00 peter 22/08 4:30, peter 22/08 5:00 etc

 $sql = "SELECT * FROM agent_schedule_table 
                      INNER JOIN agent 
                      ON agent.agent_id = agent_schedule_table.agent_id 
                     INNER JOIN property
                     ON property.agent_id = agent.agent_id
                     INNER JOIN rent_interest
                     ON rent_interest.Property_number = property.Property_number
                     
                     WHERE (agent_schedule_table.agent_schedule_date >= '$todaysdate' AND property.Property_number = '".$_SESSION["apartment_number"]."')
                     ORDER BY agent_schedule_table.agent_schedule_date ASC";
                        
                          $stmt = $pdo->prepare($sql);
                          $stmt ->execute(array());
                          $rows = array();
                                while ($row = $stmt->fetch(PDO::FETCH_ASSOC) ) {
    
                                    $result = $row;
                                    }


  • Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-warning-undefined-arr) – Markus Zeller Aug 15 '22 at 16:45
  • `$string = strval($string);` should work. You say you tried this, why didn't it work? – Barmar Aug 15 '22 at 16:46
  • What does the `foreach` loop have to do with this? It never calls `strToHex()`. – Barmar Aug 15 '22 at 16:47
  • @Barmar I was just looking for solution or anything that will suggest a solution. – Ezeomeke Stephen Ozioma Aug 15 '22 at 16:54
  • And how does adding irrelevant code to the question help you get a solution? – Barmar Aug 15 '22 at 16:56
  • When I add `$string = strval($string);` to the beginning of the function it returns the expected result. Why does the question say that this doesn't work? – Barmar Aug 15 '22 at 16:57
  • You deleted the `strToHex()` function from your question, why?e Which line in the remaining code is getting the error? – Barmar Aug 15 '22 at 17:11
  • This line gets the error: $end_time = strtotime($schedule_row["agent_schedule_end_time"] . ':00'); – Ezeomeke Stephen Ozioma Aug 15 '22 at 17:18
  • @Barmar strToHex() was not originally in my code. i was just trying to see if it will work for me. My code is foreach() and first line gives the error. While my var_dump() gives this 2022-08-18" ["agent_schedule_day"]=> string(8) "Thursday" ["agent_schedule_start_time"]=> string(5) "08:35" ["agent_schedule_end_time"]=> string(5) "22:35" ["average_inspection_time"]=> int(60) – Ezeomeke Stephen Ozioma Aug 15 '22 at 17:21
  • That means that `$schedule_row` isn't an array, it's a number. – Barmar Aug 15 '22 at 17:21
  • Please post the output of `var_dump($schedule_data);` – Barmar Aug 15 '22 at 17:22
  • @Barmar var_dump($schedule_data); is the one array(8){.....}. it is there – Ezeomeke Stephen Ozioma Aug 15 '22 at 20:23
  • Your code expects `$schedule_data` to be a 2-dimensional array. `$schedule_row` is one element of the array, and then it tries to index it with `$schedule_row["agent_schedule_end_time"]`. That's an index in `$schedule_data`, not `$schedule_row`. – Barmar Aug 15 '22 at 20:26

1 Answers1

0

I don't think you need a loop. $schedule_data is an associative array, just access the element from there, not from a nested element.

$end_time = strtotime($schedule_data["agent_schedule_end_time"] . ':00');
$start_time = strtotime($schedule_data["agent_schedule_start_time"] . ':00');
$total_agent_available_minute = ($end_time - $start_time) / 60;
$average_inspection_time = $schedule_data["average_inspection_time"];
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • thank you so far for your help. Please, dont be offended. I am more confused now. Can you help me a little bit by showing me how to add function strToHex($string) { } to my code above. I added the irrelevant code to show that i have searched for similar problems and i got something that looks similar but didnt fit as a solution. this is because it is recommended to search for similar problem before asking. Please, help me on how to implement solution to my code – Ezeomeke Stephen Ozioma Aug 15 '22 at 17:08
  • thanks, i have seen it. the error is gone but my code is not working. i am searching to see if i can identify the problem. it is a long code. If there is a way you will help me look at it, i will appreciate. – Ezeomeke Stephen Ozioma Aug 15 '22 at 17:50
  • I asked you above to post the `var_dump`. – Barmar Aug 15 '22 at 17:53
  • i will edit my question to show var_dump – Ezeomeke Stephen Ozioma Aug 15 '22 at 17:56
  • I don't see the problem. `var_dump($end_time)` looks correct. What are you expecting instead? – Barmar Aug 15 '22 at 19:22
  • Please, is it possible to chat with you? Not in this comment section – Ezeomeke Stephen Ozioma Aug 16 '22 at 16:05
  • Sorry, you don't have enough reputation to use the chat system. I'm not going to do private chat. – Barmar Aug 16 '22 at 16:08
  • Ok, please, one more request, can i edit my question so that you help me look at it. it is different from the first question. I am having a serious challenge and i learned coding online. do not have someone to share with. i only relay on here. – Ezeomeke Stephen Ozioma Aug 16 '22 at 16:48
  • No, one question at a time. If you have a new problem, post a new question. This one is already answered. – Barmar Aug 16 '22 at 16:51