Jump to content

[SOLVED] MultiDimensionalArrays - PM System


tarun

Recommended Posts

My Website Has A PM (PrivateMessaging) System

It Will Use Arrays And The PMs Are Stored In: msgs/$user.php

And Access Using: include("msgs/$user.php");

 

<?php
$msg = array(
  'MsgTitle1'=>array(
    'From1',
    'MsgBody1',
    'TimeReceived1'
  ),
  'MsgTitle2'=>array(
    'From2',
    MsgBody2',
    'TimeReceived2'
  ),
  'MsgTitle3'=>array(
    'From3',
    'MsgBody3',
    'TimeReceived3'
  )
);
?>

 

 

What What The send.php, inbox.php, delete.php Page Look Like

In Other Words How Do I View All The Array Contents (foreach?) And Delete And Add To The Array

 

 

Thnx,

Tarun

Link to comment
https://forums.phpfreaks.com/topic/42149-solved-multidimensionalarrays-pm-system/
Share on other sites

<?php
$msg = array(
  'MsgTitle1'=>array(
    'From1',
    'MsgBody1',
    'TimeReceived1'
  ),
  'MsgTitle2'=>array(
    'From2',
    'MsgBody2',
    'TimeReceived2'
  ),
  'MsgTitle3'=>array(
    'From3',
    'MsgBody3',
    'TimeReceived3'
  )
);

/**
* to list the array
*/

foreach ($msg as $title => $mdata) {
    echo "<br><b>Message title : $title</b><br>";
    foreach ($mdata as $item) {
        echo "$item<br>";
    }    
}

/**
* add new item
*/

$msg['MsgTitle4'] = array ('From4', 'MsgBody4', 'TimeReceived4');

/**
* delete msg2
*/

unset ($msg['MsgTitle2']);
?>

Don't use the message titles as the offsets, use integers, such as:

<?php
$messageList = array(
     array('title' => 'Message title',
             'sender' => 'sender',
             'body' => 'Body'),
     array('title' => 'Message title',
             'sender' => 'sender',
             'body' => 'Body'));
?>

If you use titles as offsets and two people send a message with the same title, the older one will be removed from the array.

There won't be two in there with the same title as the second would have overwritten the first when added

If he's simply using fwrite(), then no.  It would just append the data to the end of the file.  But you still just proved my point.  The second message would replace the first, whether it overwrites it from the file, or just doesn't display when printed...either way the first one is gone.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.