0

I need to show on an html a list of all the instructors that are set up on the list

pragma solidity ^0.4.18;

contract Owned {
address owner;

function Owned() public {
    owner = msg.sender;
}
modifier onlyOwner {
   require(msg.sender == owner);
   _;
 }
}

contract Courses is Owned {

struct Instructor {
    uint age;
    bytes16 fName;
    bytes16 lName;
}

mapping (address => Instructor) instructors;
address[] public instructorAccts;

event instructorInfo(
   bytes16 fName,
   bytes16 lName,
   uint age
);

function setInstructor(address _address, uint _age, bytes16 _fName, bytes16 _lName) onlyOwner public {
    var instructor = instructors[_address];

    instructor.age = _age;
    instructor.fName = _fName;
    instructor.lName = _lName;

    instructorAccts.push(_address)-1;
    instructorInfo(_fName, _lName, _age);
}

function getInstructors() view public returns(address[]) {
    return instructorAccts;
}

function getInstructor(address _address) view public returns (uint, bytes16, bytes16) {
    return (instructors[_address].age, instructors[_address].fName, instructors[_address].lName);
}

function countInstructors() view public returns (uint) {
    return instructorAccts.length;
}

}

UPDATE: my web3js code

  <script>
       if (typeof web3 !== 'undefined') {
    web3 = new Web3(web3.currentProvider);
} else {
    web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
}

web3.eth.defaultAccount = web3.eth.accounts[1];

var CoursetroContract = web3.eth.contract([
{
    "constant": true,
    "inputs": [
        {
            "name": "_address",
            "type": "address"
        }
    ],
    "name": "getInstructor",
    "outputs": [
        {
            "name": "",
            "type": "uint256"
        },
        {
            "name": "",
            "type": "bytes16"
        },
        {
            "name": "",
            "type": "bytes16"
        }
    ],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
},
{
    "constant": true,
    "inputs": [],
    "name": "getInstructors",
    "outputs": [
        {
            "name": "",
            "type": "address[]"
        }
    ],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
},
{
    "constant": true,
    "inputs": [
        {
            "name": "_index",
            "type": "uint256"
        }
    ],
    "name": "getInstructorAtIndex",
    "outputs": [
        {
            "name": "",
            "type": "uint256"
        },
        {
            "name": "",
            "type": "bytes16"
        },
        {
            "name": "",
            "type": "bytes16"
        }
    ],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
},
{
    "constant": true,
    "inputs": [
        {
            "name": "",
            "type": "uint256"
        }
    ],
    "name": "instructorAccts",
    "outputs": [
        {
            "name": "",
            "type": "address"
        }
    ],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
},
{
    "constant": true,
    "inputs": [],
    "name": "countInstructors",
    "outputs": [
        {
            "name": "",
            "type": "uint256"
        }
    ],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
},
{
    "constant": false,
    "inputs": [
        {
            "name": "_address",
            "type": "address"
        },
        {
            "name": "_age",
            "type": "uint256"
        },
        {
            "name": "_fName",
            "type": "bytes16"
        },
        {
            "name": "_lName",
            "type": "bytes16"
        }
    ],
    "name": "setInstructor",
    "outputs": [],
    "payable": false,
    "stateMutability": "nonpayable",
       "type": "function"
},
{
    "anonymous": false,
    "inputs": [
        {
            "indexed": false,
            "name": "fName",
            "type": "bytes16"
        },
        {
            "indexed": false,
            "name": "lName",
            "type": "bytes16"
        },
        {
            "indexed": false,
            "name": "age",
            "type": "uint256"
        }
    ],
    "name": "instructorInfo",
    "type": "event"
        }
     ]);

  var Coursetro = 
  CoursetroContract.at('0x13fd9e45bbd7ee860cdfc825839c9dfbd91a36cd');
  var instructorEvent = Coursetro.instructorInfo({}, 'EARLIEST');

instructorEvent.watch(function(error, result) {
    if (result) {
        if (result.blockHash != $("#insTrans").html())
             $("#loader").hide();
         $("#insTrans").html('Block hash: ' + result.blockHash);
        $("#instructor").html(web3.toAscii(result.args.fName) + ' ' + web3.toAscii(result.args.lName) + ' (' + result.args.age + ' Kb)');
    } else {
         $("#loader").hide();
    }
});

Coursetro.countInstructors((err, res) => {
    if (res)
     $("#countIns").html(res.c + ' number of files');
})

$("#button").click(function() {
     //$("#loader").show();
    Coursetro.setInstructor(web3.eth.defaultAccount, $("#age").val(), $("#fName").val(), $("#lName").val(), (err, res) => {
        if (err) {
            $("#loader").hide();
        }
    });
});

0

To show a list of your instructors you will need to add another function to your smart contract:

function getInstructorAtIndex(uint _index) view public returns (uint, bytes16, bytes16) {
    Instructor storage i = instructors[instructorAccts[index]];
    return (i.age, i.fName, i.lName);
}

Then you will have to deploy your smart contract on a ethereum blockchain. Firstly try it in a testnet like Ropsten. You can manage with Remix framework and Metamask wallet. (https://remix.ethereum.org/) (https://metamask.io/)

Finally you will have to build an application. Web3j is the main connector and it has three implementations: javascript, java and scala. Most of the people uses Web3js (https://github.com/ethereum/web3.js/) to build Dapps. In this way, you can use Truffle framework as well (http://truffleframework.com). Truffle has some boiler-plates to develop a sample project quickly.

I hope I help you. :-)

PS: You can set your mapping and array as private in your smart contract. Like you have it the EVM will automatically create getters for them.

PSS: You can set the "constant" or "pure" modifier to your getters functions.

  • Great thanks, but how do I use it with web3js? Im using node too. – Victor Joel Cuadros Choez May 2 '18 at 23:45
  • I update the post with the web3js code – Victor Joel Cuadros Choez May 2 '18 at 23:54
  • @VictorJoelCuadrosChoez sorry, but I am not so familiar with jquery and javascript programming. And if you dont give us the whole context is so difficult to help you. Just search for info about web3js, and about loops and divs creation with jquery and js. In Internet you can find useful examples of both of them. Good luck :) – miguelmartinezinf May 3 '18 at 14:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.